如何在继承XAML ComboBox的自定义用户控件中设置所选属性?

时间:2009-09-29 14:51:04

标签: wpf xaml combobox

我已经构建了一个自定义用户控件,它继承了ComboBox并且具有

的项目
KeyValuePair<bool, string>

我想设置此ComboBox的布尔默认值,以便当 true 时显示“”,当显示为false时显示“ ”。

在下面的代码中,当我将所选值设置为 true 时,ComboBox会正确显示“是”。

但是当我将所选值设置为 false 时,ComboBox仍为空白。

我需要对此用户控件做什么,以便在将所选值设置为false时显示“否”?

Window1.xaml:

<Window x:Class="TestYesNoComboBox234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestYesNoComboBox234"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Contract available?" Margin="0 0 10 0"/>
            <local:YesNoComboBox Width="60" Height="22" 
               x:Name="ContractAvailable"/>
        </StackPanel>
    </StackPanel>
</Window>

Window1.xaml.cs:

using System.Windows;

namespace TestYesNoComboBox234
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            //ContractAvailable.SelectedValue = true; //correctly sets "Yes"
            ContractAvailable.SelectedValue = false; //incorrectly does not select anything
        }
    }
}

YesNoComboBox.xaml:

<ComboBox x:Class="TestYesNoComboBox234.YesNoComboBox"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Value}" />
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

YesNoComboBox.xaml.cs:

using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;

namespace TestYesNoComboBox234
{
    public partial class YesNoComboBox : ComboBox
    {

        public YesNoComboBox()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(YesNoComboBox_Loaded);
        }

        void YesNoComboBox_Loaded(object sender, RoutedEventArgs e)
        {
            SelectedValuePath = "Key";
            Items.Add(new KeyValuePair<bool, string>(true, "Yes"));
            Items.Add(new KeyValuePair<bool, string>(false, "No"));
        }

    }
}

1 个答案:

答案 0 :(得分:1)

发现问题。窗口构造函数发生在YesNoComboBox_Loaded之前。通过在组合框实际具有值之前设置所选值,由于某种原因它只能选择第一个项目(尝试颠倒你添加KeyValuePairs的方式并将SelectedValue设置为false,然后尝试为true.False将起作用,true将不会)。我也尝试在Window_Loaded事件中执行此操作,但Window_Loaded也在YesNoComboBox_Loaded之前发生。所以你需要做的是在组合框的构造函数中添加KeyValuePair项,然后在Window_Loaded中设置所选的值(因为窗口构造函数也发生在你的组合框构造函数之前)。这是代码:

窗口:

public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
    }

    private void window1_Loaded(object sender, RoutedEventArgs e)
    {
        ContractAvailable.SelectedValue = true;
    }
}

YesNoComboBox:

public partial class YesNoComboBox : ComboBox
{
    public YesNoComboBox()
    {
        InitializeComponent();

        SelectedValuePath = "Key";

        Items.Add(new KeyValuePair<bool, string>(true, "Yes"));
        Items.Add(new KeyValuePair<bool, string>(false, "No"));
    }
}