列表框选择的项目返回null值



i有一个列表框,它绑定到viewModel,我使用鼠标_doubeclickevent来检索所选的项目值,但是它的返回null,我在这里可能会缺少什么?swdistinct是一个列表

ViewModel:

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

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

用于将selectedItem传递给DoubleClick上的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);
    }
}

最新更新