【问题标题】:Setting ComboBox ItemSource/SelectedValue in custom control在自定义控件中设置 ComboBox ItemSsource/SelectedValue
【发布时间】:2017-02-06 16:42:25
【问题描述】:

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

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

我正在尝试创建一个通用的自定义用户控件。为此,我必须在所有代码中都这样做。我的自定义控件只是一个文本框和一个组合框。组合框应包含枚举中的项目(在通用中指定)。这些项目将是要显示的单位列表(英尺/米、度/弧度等)。如果用户更改单位,文本框中的值将被转换(使用 MultiValueConverter) 我已经使用 here 描述的方法通过 XAML 完成了组合枚举绑定。而且,我已经让控件在没有英尺/米单位控件的通用控件的情况下工作。但是,现在我不知道如何在泛型的构造函数中设置 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
}

【问题讨论】:

    标签: c# wpf combobox binding


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 那行得通。我什至没有直接获得枚举值。我试图使用我必须为 XAML 创建的东西。
    猜你喜欢
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2018-01-09
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 2016-06-07
    相关资源
    最近更新 更多