WPF用户控制问题

时间:2018-01-22 05:56:49

标签: c# wpf xaml

我正在尝试将一些文本框组合到用户控件中,但是我的observablecollection没有显示到组合框,有人可以看看吗?

我要做的是创建一个填充的组合框,并将selectedvalue绑定回主窗口

public partial class uc1 : UserControl
{
    public ObservableCollection<Item1> Item1s { get; set; }

    public string Display1 { get; set; }

    public Item1 Item
    {
        get { return (Item1)GetValue(ItemProperty); }
        set { SetValue(ItemProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Item.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ItemProperty =
        DependencyProperty.Register("Item", typeof(Item1), typeof(uc1), new PropertyMetadata(default(Item1)));

    public uc1()
    {
        InitializeComponent();

        if (!ViewModelBase.IsInDesignModeStatic)
        {
             Init Item1s
        }
    }

以下是xaml

<UserControl x:Name="pp">
<Grid DataContext="{Binding ElementName=pp}">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>

    <ComboBox Height="25"
              Margin="5,0"
              DisplayMemberPath="Property1"
              ItemsSource="{Binding Item1s}"
              SelectedValue="{Binding Item}" />       

</Grid>

0 个答案:

没有答案