wpf多列组合框将所选内容绑定到itemssource集合的子类

wpf多列组合框将所选内容绑定到itemssource集合的子类,wpf,binding,combobox,subclass,selecteditem,Wpf,Binding,Combobox,Subclass,Selecteditem,假设我们有以下模型: public class MyClass : BaseModel { public Item Item1 {get ...; set ...;} public Item Item2 {get ...; set ...;} public Item Item3 {get ...; set ...;} public Item Item4 {get ...; set ...;} public ObservableCollection<I

假设我们有以下模型:

public class MyClass : BaseModel
{
    public Item Item1 {get ...; set ...;}
    public Item Item2 {get ...; set ...;}
    public Item Item3 {get ...; set ...;}
    public Item Item4 {get ...; set ...;}
    public ObservableCollection<ItemContainer> AllItemContainers {get ...; set ...;}
}

public class ItemContainer : BaseModel
{
    public Item Item {get ...; set ...;}
    public MyEnum EnumProp {get ...; set ...;}
    public byte? SomeProp {get ...; set ...;}
}

public class Item : BaseModel
{
    public string Name {get ...; set ...;}
    public int Id {get ...; set ...;}
}
到目前为止,它运行良好,组合框显示了正确的Itemname,我可以从下拉列表中选择一个项目并将其分配给Item1属性

var items = new ObservableCollection<Item>();            
foreach (ItemContainer ic in Model.MyClass.AllItemContainers)
{
    items.Add(ic.Item);
}
现在我使用了一个多列组合框,如下所示:

<ComboBox Width="150" Margin="5" HorizontalContentAlignment="Stretch"
          ItemsSource="{Binding Model.MyClass.AllItemContainers}">                    
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Margin="2" Text="{Binding Path=Item.Name}"/>
    </DataTemplate>
  </ComboBox.ItemTemplate>
  <ComboBox.ItemContainerStyle>
    <Style TargetType="{x:Type ComboBoxItem}">
      <Setter Property="MinWidth" Value="250"/>
        <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate>
              <Grid Width="{Binding ActualWidth, RelativeSource={RelativeSource
                    FindAncestor, AncestorType={x:Type ComboBoxItem}}, Mode=OneTime}">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="*"/>
                  <ColumnDefinition Width="0.5*"/>
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Path=Item.Name}"/>
                <TextBlock Grid.Column="1" Text="{Binding Path=EnumProp,
                           Converter={StaticResource enumToStringConverter}}"
                           HorizontalAlignment="Center"/>
                <TextBlock Grid.Column="2" Text="{Binding Path=SomeProp}"/>
              </Grid>
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </ComboBox.ItemContainerStyle>
  </ComboBox>

这很好,我在组合框的文本框中只看到名称,我的下拉列表中有3列

但是现在我显然不能直接绑定SelectedItem,因为绑定的目的地是Item类型,而SelectedItem是ItemContainer类型

<Combobox SelectedItem="Model.MyClass.Item1"/>

我几乎试过任何设置

<Combobox SelectedValuePath="Item"/>

编写这样的转换器

public class ItemContainerToItemConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var item = value as Item;
        var containers = parameter as ObservableCollection<ItemContainer>;
        if (item != null && containers != null)
            return containers.FirstOrDefault(f => f.Item.Id == item.Id);
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var container = value as ItemContainer;
        if (container != null)
            return container.Item;
        return null;            
    }
}
公共类ItemContainerToItemConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
var项目=作为项目的价值;
var containers=作为ObservableCollection的参数;
if(项!=null&&containers!=null)
返回containers.FirstOrDefault(f=>f.Item.Id==Item.Id);
返回null;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
var container=作为ItemContainer的值;
if(容器!=null)
返回容器。项目;
返回null;
}
}
我试图绑定ConverterParameter,但由于它不是DependencyProperty,因此无法绑定

<ComboBox SelectedItem="{Binding Model.MyClass.Item1,
          Converter={StaticResource itemConverter},
          ConverterParameter={Binding Model.MyClass.AllItemContainers}}"/>

我现在没有主意了,我不想使用SelectionChanged事件(甚至不想使用EventToCommand),在我的viewmodel中分配值,并侦听PropertyChanged事件以刷新组合框

有没有其他方法来实现这一点,或者我遗漏了什么


非常感谢您的帮助。

您可以尝试设置
SelectedValuePath
,并将
SelectedItem
属性绑定到模型/视图模型中的
Item
类型的属性,而不是
SelectedValue

<ComboBox Width="150" Margin="5" HorizontalContentAlignment="Stretch" 
          ItemsSource="{Binding AllItemContainers}" 
          SelectedValuePath="Item"
          SelectedValue="{Binding Item1, Mode=TwoWay}">


不使用PropertyChanged事件有什么具体原因吗?你会节省很多时间,而且它更易于维护。@大多数情况下,我只是认为这不是mvvm的方法,我只是想使用绑定,不让viemodel更改项。非常感谢,我不知道为什么我自己没有尝试,但它正在工作。