wpf组合框-绑定自定义isselected属性

wpf组合框-绑定自定义isselected属性,wpf,combobox,selectedvalue,Wpf,Combobox,Selectedvalue,我想将属性为“ID”、“Description”和“IsSelected”的项目列表绑定到组合框。使用DisplayMemberPath将显示值设置为“Description”,这样可以正常工作。但是,我希望在选择该项时设置“IsSelected”属性。我已尝试将SelectedValuePath和SelectedValue设置为“IsSelected”,但不起作用。最简单的解决方案可能是跟踪视图模型中的选定项,并通过向SelectedItem添加双向绑定,使其与组合框保持同步。当视图模型属性更

我想将属性为“ID”、“Description”和“IsSelected”的项目列表绑定到组合框。使用DisplayMemberPath将显示值设置为“Description”,这样可以正常工作。但是,我希望在选择该项时设置“IsSelected”属性。我已尝试将SelectedValuePath和SelectedValue设置为“IsSelected”,但不起作用。

最简单的解决方案可能是跟踪视图模型中的选定项,并通过向SelectedItem添加双向绑定,使其与组合框保持同步。当视图模型属性更改时,更新新选择和以前选择的IsSelected属性。

尝试此操作

    <ComboBox Width="120" Height="35">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ComboBoxItem IsSelected="{Binding IsSelected}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

再没有比回答一个五年前的问题更重要的了。。。我不同意Mike的观点,因为您应该跟踪这两种状态,因为组合框可以为您提供SelectedValue的IsSelected状态。矩阵在正确的行上,但不能将IsSelected与DisplayMemberPath一起使用

使用下面的代码,my Fruits Selected属性绑定到IsSelected

视图代码

BaseViewModel


所选项目不是其ID或说明。
<ComboBox ItemsSource="{Binding Fruit}"
          SelectedValue="{Binding SelectedFruitViewModel}" 
          DisplayMemberPath="Name">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="IsSelected" Value="{Binding Selected}" />
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>
public class MainWindowViewModel : BaseViewModel
{
    private FruitViewModel _selectedFruitViewModel;

    public MainWindowViewModel()
    {
        this.Fruit = new ObservableCollection<FruitViewModel>
        {
            new FruitViewModel { Name = "Pineapple", Selected = false },
            new FruitViewModel { Name = "Apple", Selected = false },
            new FruitViewModel { Name = "Orange", Selected = false },
            new FruitViewModel { Name = "Kiwi", Selected = false }
        };
    }

    public ObservableCollection<FruitViewModel> Fruit { get; }

    public FruitViewModel SelectedFruitViewModel
    {
        get => _selectedFruitViewModel;
        set => SetProperty(ref _selectedFruitViewModel, value);
    }
}
public class FruitViewModel : BaseViewModel
{
    private bool _selected;
    private string _name;

    public bool Selected
    {
        get => _selected;
        set => SetProperty(ref _selected, value);
    }

    public string Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }
}
public class BaseViewModel : INotifyPropertyChanged 
{
    public virtual event PropertyChangedEventHandler PropertyChanged;

    protected virtual bool SetProperty<T>(ref T storage, 
                                          T value, 
                                          [CallerMemberName] string propertyName = null)
    {
        if (EqualityComparer<T>.Default.Equals(storage, value))
        {
            return false;
        }

        storage = value;

        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));

        return true;
    } 
}