在自定义控件中设置ComboBox ItemSource / SelectedValue

时间:2017-02-06 16:42:25

标签: c# wpf combobox binding

如何在代码中复制以下代码(不是XAML)?

<ComboBox Name="DisplayValueComboBox" Grid.Column="1" ItemsSource="{Binding Source={wpfUtil:EnumBindingSource {x:Type unitTypes:FeetOrMetersSelectionType}}}" SelectedValue="{Binding Path=DisplayValueType}"></ComboBox>

我正在尝试创建一个通用的自定义用户控件。要做到这一点,我必须在所有代码中执行此操作。我的自定义控件只是一个文本框和一个组合框。组合框应包含枚举中的项目(在通用中指定)。这些项目将是要显示的单位列表(英尺/米,度/弧度等)。如果用户更改单位,则将转换文本框中的值(使用MultiValueConverter)我已通过XAML使用描述的here方法完成了组合枚举绑定。而且,我的控制工作没有通用的英尺/米单位控制。但是,现在我迷失了如何在泛型的构造函数中设置ItemSource和SelectedValue。

如果它有帮助,下面是基础泛型的其余部分和子类:

/// <summary>
/// Control that displays value in different units depending on selected unit type.
/// </summary>
/// <typeparam name="TDisplayTypeEnum">The enumeration type for all the available units.</typeparam>
/// <typeparam name="TConverterType">The MultiValueConverter that converts the value between the different types of units.</typeparam>
/// <typeparam name="TValueType">The underlying type of the stored value.</typeparam>
public class UnitControlBase<TDisplayTypeEnum, TConverterType, TValueType> : UserControl
    where TDisplayTypeEnum : struct, IConvertible
    where TConverterType : IMultiValueConverter, new()
{
    /// <summary>
    /// Constructor.
    /// </summary>
    public UnitControlBase()
    {
        Grid mainGrid = new Grid();
        mainGrid.Name = "LayoutRoot";
        this.AddChild(mainGrid);

        ColumnDefinition col1 = new ColumnDefinition();
        ColumnDefinition col2 = new ColumnDefinition();
        mainGrid.ColumnDefinitions.Add(col1);
        mainGrid.ColumnDefinitions.Add(col2);

        TextBox displayValueTextBox = new TextBox();
        displayValueTextBox.Name = "DisplayValueTextBox";
        MultiBinding mb = new MultiBinding();
        mb.Converter = new TConverterType();
        mb.Bindings.Add(new Binding("Value"));
        mb.Bindings.Add(new Binding("ValueType"));
        mb.Bindings.Add(new Binding("DisplayValueType"));
        displayValueTextBox.SetBinding(TextBox.TextProperty, mb);
        Grid.SetColumn(displayValueTextBox, 0);
        mainGrid.Children.Add(displayValueTextBox);

        ComboBox displayValueComboBox = new ComboBox();
        displayValueComboBox.Name = "DisplayValueComboBox";            
        //displayValueComboBox.ItemsSource = ???
        //displayValueComboBox.SelectedValue = ???
        Grid.SetColumn(displayValueComboBox, 1);
        mainGrid.Children.Add(displayValueComboBox);            
    }

    private static FrameworkPropertyMetadata valuePropertyMetadata = new FrameworkPropertyMetadata(default(TValueType));

    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(TValueType), typeof(UnitControlBase<TDisplayTypeEnum, TConverterType, TValueType>), valuePropertyMetadata);

    public TValueType Value
    {
        get
        {
            return (TValueType)GetValue(ValueProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    private static FrameworkPropertyMetadata valueTypePropertyMetadata = new FrameworkPropertyMetadata(default(TDisplayTypeEnum));

    public static readonly DependencyProperty ValueTypeProperty =
        DependencyProperty.Register("ValueType", typeof(TDisplayTypeEnum), typeof(UnitControlBase<TDisplayTypeEnum, TConverterType, TValueType>), valueTypePropertyMetadata);

    public TDisplayTypeEnum ValueType
    {
        get
        {
            return (TDisplayTypeEnum)GetValue(ValueTypeProperty);
        }
        set
        {
            SetValue(ValueProperty, value);
        }
    }

    private static FrameworkPropertyMetadata displayValueTypePropertyMetadata = new FrameworkPropertyMetadata(default(TDisplayTypeEnum));

    public static readonly DependencyProperty DisplayValueTypeProperty =
        DependencyProperty.Register("DisplayValueType", typeof(TDisplayTypeEnum), typeof(UnitControlBase<TDisplayTypeEnum, TConverterType, TValueType>), displayValueTypePropertyMetadata);

    public TDisplayTypeEnum DisplayValueType
    {
        get
        {
            return (TDisplayTypeEnum)GetValue(DisplayValueTypeProperty);
        }
        set
        {
            SetValue(DisplayValueTypeProperty, value);
        }
    }   
}

儿童班:

public class FeetMetersControlBase : UnitControlBase<FeetOrMetersSelectionType, FeetToMetersMultiValueConverter, double>
{
}

英尺/米类型:

public enum FeetOrMetersSelectionType
{
    Feet,
    Meters
}

1 个答案:

答案 0 :(得分:1)

试试这个:

displayValueComboBox.ItemsSource = Enum.GetValues(typeof(TDisplayTypeEnum));
displayValueComboBox.SetBinding(ComboBox.SelectedItemProperty, new Binding("DisplayValueType") { Source = this });