WPF组合框项目未显示

时间:2014-06-03 08:47:52

标签: wpf xaml combobox

我的combobox itemssource是usercontrol的集合。 弹出下拉列表显示良好,当我单击组合框的箭头按钮,但没有在任何项目中显示组合框的文本块区域。谁知道原因?

MainWindow.xaml

<ComboBox x:Name="myCombo" ItemsSource="{Binding UserControlList}" SelectedItem="{Binding SelectedUserControl}" >
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=MyItemName}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

MainWindow.xaml.cs

ObservableCollection<MyUserControl> _UserControlList = new ObservableCollection<MyUserControl>();
    public ObservableCollection<MyUserControl> UserControlList
    {
        get
        {
            return _UserControlList;
        }
        set
        {
            _UserControlList = value;
            OnPropertyChanged();
        }
    }
    MyUserControl _SelectedUserControl;
    public MyUserControl SelectedUserControl
    {
        get { return _SelectedUserControl; }
        set
        {
            _SelectedUserControl = value;
            OnPropertyChanged();
        }
    }

...

public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;

        UserControlList.Add(u1);
        UserControlList.Add(u2);
        UserControlList.Add(u3);
    }

    MyUserControl u1 = new MyUserControl { MyItemName = "Apple" };
    MyUserControl u2 = new MyUserControl { MyItemName = "Banana" };
    MyUserControl u3 = new MyUserControl { MyItemName = "Lemon" };

MyUserControl.xaml.cs

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();

    }

    public string MyItemName { get; set; }
}

0 个答案:

没有答案