列表框选中的项返回空值

时间:2017-10-02 18:15:58

标签: c# wpf mvvm data-binding

我有一个绑定到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());
    }
}

2 个答案:

答案 0 :(得分:2)

SelectedItem将为您绑定的对象提供。在这种情况下,它将成为逆转的对象。请尝试以下代码。

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());
    }
}

答案 1 :(得分:0)

在doubleclick上将selecteditem传递给viewmodel的示例。请尝试以下代码。

<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);
    }
}