【问题标题】:listbox selected item returning Null value列表框选定项返回 Null 值
【发布时间】:2017-10-02 18:15:57
【问题描述】:

我有一个绑定到 Viewmodel 的列表框,我正在使用 mouse_doubeclickevent 来检索所选项目的值,但它返回 null,我可能在这里缺少什么? SWdistinct 是一个列表

视图模型:

public List<swversion> SWdistinct
{
    get;
    set;
}

XAML:

<ListBox x:Name="CRSWUNIQUE" ItemsSource="{Binding SWdistinct}"  SelectedItem="{Binding Path=SWdistinct,Mode=TwoWay}"  MouseDoubleClick="CRSWUNIQUE_MouseDoubleClick"   DisplayMemberPath="SW_Version" IsTextSearchEnabled="True"   />

代码背后:

private void CRSWUNIQUE_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    ListBoxItem item = CRSWUNIQUE.SelectedItem as ListBoxItem;
    if (item != null)
       // if (CRSWUNIQUE.SelectedItem != null)
    {            
        MessageBox.Show(item.Content.ToString());
    }
}

【问题讨论】:

  • 应该ItemsSourceSelectedItem 都是SWdistinct 吗?您确定 SelectedItem 在您的双击处理程序中实际上是 null 吗?返回 null 的可能是 ListBoxItem 的强制转换。在调试时检查 SelectedItem 有什么?
  • @TylerLee SWdistinct 是一个列表,当我调试此行并将光标指向 CRSWUNIQUE 时,它显示如下System.Windows.Controls.ListBoxItem.count:2,当我指向 SelectedItem 时,它显示了值SW_Version:myvalue

标签: c# wpf mvvm data-binding


【解决方案1】:

SelectedItem 会给你绑定的对象。在这种情况下,它将是 swversion 的对象。试试下面的代码。

private void CRSWUNIQUE_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
   var swversionitem = CRSWUNIQUE.SelectedItem as swversion;
    if (swversionitem != null)
       // if (CRSWUNIQUE.SelectedItem != null)
    {            
        MessageBox.Show(swversionitem.SW_Version.ToString());
    }
}

【讨论】:

  • 非常感谢,这很完美。我只需要把 var 放在 swversionitem 之前。有没有办法在 viewmodel 中触发 Mousedoubleclick 并将 selectedvalue 传递给 viewmodel ?
  • 是的,您可以触发双击视图模型。将其作为一个单独的问题发布。我会回答的。
  • 谢谢,但它不允许我发布问题,我应该等待 90 分钟 :)
  • @SamKing 我创建了一个示例,用于在双击时将选定项传递给 viewmodel。我将在这篇文章中作为单独的答案发布
  • @SamKing 检查下面的答案或在双击时将选定项传递给视图模型
【解决方案2】:

双击时将选定项传递给视图模型的示例。试试下面的代码。

<ListBox ItemsSource="{Binding Swversions}" x:Name="lstListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding SW_Version}">
                    <TextBlock.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick" 
                                      Command="{Binding ElementName=lstListBox,Path=DataContext.ListDoubleClickCommand}"
                                      CommandParameter="{Binding ElementName=lstListBox, Path=SelectedItem}"/>
                    </TextBlock.InputBindings>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();
        DataContext = new ViewModel();
    }
}

class ViewModel
{
    public List<swversion> Swversions { get; set; }

    public ICommand ListDoubleClickCommand { get; set; }
    public ViewModel()
    {
        Swversions = new List<swversion>()
        {
            new swversion() {SW_Version = "Version1"},
            new swversion() {SW_Version = "Version2"},
            new swversion() {SW_Version = "Version3"},
            new swversion() {SW_Version = "Version4"},
            new swversion() {SW_Version = "Version5"}
        };
        ListDoubleClickCommand = new RelayCommand(OnDoubleClick);
    }

    private void OnDoubleClick(object parameter)
    {

    }
}

class swversion
{
    public string SW_Version { get; set; }
}
public class RelayCommand : ICommand
{
    private Action<object> execute;
    private Func<object, bool> canExecute;

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public RelayCommand(Action<object> execute, Func<object, bool> canExecute = null)
    {
        this.execute = execute;
        this.canExecute = canExecute;
    }

    public bool CanExecute(object parameter)
    {
        return this.canExecute == null || this.canExecute(parameter);
    }

    public void Execute(object parameter)
    {
        this.execute(parameter);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2018-01-30
    相关资源
    最近更新 更多