【问题标题】:wpf multi column combobox bind selection to a subclass of itemssource collectionwpf多列组合框将选择绑定到itemssource集合的子类
【发布时间】:2014-04-17 21:55:37
【问题描述】:

假设我们有以下模型:

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

所有属性都有 INPC(在 BaseModel 中实现)。

我有 4 个组合框,分别显示 Item1、Item2 等,对于 ItemsSource,我在我的视图模型中创建了一个项目列表,该列表是从 AllItemContainers 属性创建的。

var items = new ObservableCollection<Item>();            
foreach (ItemContainer ic in Model.MyClass.AllItemContainers)
{
    items.Add(ic.Item);
}

到目前为止,它运行良好,组合框显示了正确的项目名称,我可以从下拉列表中选择一个项目并将其分配给 Item1 属性。

现在我玩过这样的多列组合框:

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

我尝试绑定 ConverterParameter,但由于它不是 DependencyProperty,因此无法绑定。

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

我现在已经没有想法了,我不想使用 SelectionChanged-Event(甚至不使用 EventToCommand)并在我的视图模型中分配值并监听 PropertyChanged 事件以刷新到组合框。

还有其他方法可以做到这一点,还是我错过了什么?

非常感谢您的帮助。

【问题讨论】:

  • 不使用 PropertyChanged 事件有什么具体原因吗?你会节省很多时间,而且更易于维护。
  • @Mostey 我只是认为这不是 mvvm 的方式,我只是想要使用绑定并且不要让 viemodel 更改项目。

标签: wpf binding combobox subclass selecteditem


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,我不知道为什么我自己没有尝试过,但它确实有效。
猜你喜欢
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多