简体   繁体   English

如何将数据绑定到集合中复合对象的属性?

[英]How to data-bind to a property of a composite object in a collection?

I need to allow selection of several items from this predefined list: 我需要允许从此预定义列表中选择几个项目:

public enum QuarkType{
    Up,
    Down,
    [Description("Magical Being")] Charm,
    [Description("Quite Odd")] Strange,
    Top,
    Bottom
}

So I use CheckComboBox , and use the DescriptionAttribute where I need to use custom description. 所以我用CheckComboBox ,并使用DescriptionAttribute ,我需要使用自定义的描述。 I feed the CheckComboBox using a MarkupExtension that returns a list of all values of the given enum as IEnumerable<EnumDescriptionPair> , where EnumDescriptionPair is: 我使用MarkupExtension来提供CheckComboBox ,该MarkupExtensionIEnumerable<EnumDescriptionPair>返回给定enum的所有值的列表,其中EnumDescriptionPair为:

public class EnumDescriptionPair{
    public object Value { get; set; }
    public string Description { get; set; }
}

Now the problem is how to pass the Value s of this list to the code-behind list: 现在的问题是如何将此列表的Value传递到代码隐藏列表:

public ObservableCollection<QuarkType> SelectedQuarksList { get; set; }

I mean, how to take just the Value out of the EnumDescriptionPair for each item of the selected list ? 我的意思是,如何仅将EnumDescriptionPairValue从选定列表的每个项目中取出?

This is what I have thus far. 到目前为止,这就是我所拥有的。 It obviously doesn't work (meaning it shows the right strings in the CheckComboBox , and allows selecting several items, but isn't reflected in the SelectedQuarksList mentioned above): 它显然不起作用(这意味着它在CheckComboBox显示了正确的字符串,并允许选择多个项目,但未反映在上述的SelectedQuarksList中):

<Window x:Class="MyEditor.MainWindow"
        xmlns:loc="clr-namespace:MyEditor"
        xmlns:toolKit="clr-namespace:Xceed.Wpf.Toolkit;assembly=Xceed.Wpf.Toolkit"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <StackPanel>
        <toolKit:CheckComboBox x:Name="Ccb" Delimiter=","
                               ItemsSource="{loc:EnumItemsValueConverter {x:Type loc:QuarkType}}"
                               DisplayMemberPath="Description"
                               SelectedItemsOverride="{Binding SelectedQuarksList}" />

        <ListBox ItemsSource="{Binding SelectedQuarksList}" />
    </StackPanel>
</Window>

To do exactly what your question asks, you could try using a converter on the SelectedQuarksList binding that does a ".Select(q => q.Value)" in the ConvertBack function. 要完全按照您的问题要求进行操作,可以尝试在SelectedQuarksList绑定上使用转换器,该转换器在ConvertBack函数中执行“ .Select(q => q.Value)”。

To get the behavior you want, I have done this successfully in the past (example with 2 of your values), this sets up the enum as "Flags" so the value sequence goes 0, 1, 2, 4...: 为了获得您想要的行为,我过去已经成功完成了此操作(例如,使用您的2个值),这将枚举设置为“ Flags”,因此值序列变为0、1、2、4 ...:

<StackPanel Orientation="Horizontal">
<Checkbox Content="Up" IsChecked="{Binding Path=SelectedQuarksFlags, Converter={Static Resource HasFlagToBoolConverter}, ConverterParamater={x:Static Quarks.Up}}"
<Checkbox Content="Magical Being" IsChecked="{Binding Path=SelectedQuarksFlags, Converter={Static Resource HasFlagToBoolConverter}, ConverterParamater={x:Static Quarks.Charm}}"
</StackPanel>

The converter looks like: 转换器看起来像:

 Quark _lastSeenValue;
 public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Quark paramQuark = (Quark)parameter;
        Quark currentQuark = (Quark)value;

        _lastSeenValue = currentQuark;
        return currentQuark.HasFlag(paramQuark);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        Quark newQuark = _lastSeenValue;
        Quark paramQuark = (Quark)parameter;

        if ((bool)value)
        {
            newQuark |= paramQuark; 
        }
        else
        {
            newQuark &= ~paramQuark;
        }

        _lastSeenValue = newQuark;
        return newQuark;
    }

This could be converted to add or remove from a list relatively easily, but I know the code above works. 可以将其转换为相对容易地添加到列表或从列表中删除,但是我知道上面的代码有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM