WPF组合框与复合集合中的/ Observablecollection

时间:2014-07-17 14:33:20

标签: c# wpf combobox compositecollection

我正在尝试使用复合集合填充带有可观察集合和标题行的组合框。标题在那里,但我无法填充OC对象。我刚刚开始使用OC,所以可能是我缺少的基本内容。

客户类:

public class Customer : ViewModelBaseMain
{
    public string CustomerName { get; set; }
    public int CustomerId { get; set; }
}

我从数据表中填充OC(这是一个依赖属性)(我验证了OC正在正确填充):

ObservableCollection<Customer> dpCustomerListOC = new ObservableCollection<Customer>();
foreach (DataRow drow in dpHeaderCustTable.Rows)
{
    Customer Customer = new Customer();
    Customer.CustomerId = Convert.ToInt32(drow["customer_id"]);
    Customer.CustomerName = drow["customer_name"].ToString();
    dpCustomerListOC.Add(Customer);                            
}

在usercontrol资源中,我将VM指定为静态资源:

<local:ReservationViewModel x:Key="ReserveVM"/>

我已经尝试了两种不同的方式来阅读我的帖子。

使用x:reference:

在资源中收集第一种方式
<ComboBox x:Name="cboCustHeader" Grid.IsSharedSizeScope="True" IsEditable="False"
          ItemsSource="{DynamicResource items}">                                  
     <ComboBox.Resources>
         <CompositeCollection x:Key="items">
              <ComboBoxItem IsEnabled="False">
                  <Border Style="{StaticResource ComboHeaderBorder}">
                     <Grid Style="{StaticResource ComboHeaderStyle}">
                         <Grid.ColumnDefinitions>
                             <ColumnDefinition SharedSizeGroup="A"/>
                             <ColumnDefinition Width="7"/>
                             <ColumnDefinition SharedSizeGroup="B"/>
                         </Grid.ColumnDefinitions>
                         <Grid.Children>
                            <TextBlock Grid.Column="0" Text="ID"/>
                            <TextBlock Grid.Column="2" Text="Name"/>
                         </Grid.Children>
                     </Grid>
                  </Border>
              </ComboBoxItem>
              <CollectionContainer Collection="{Binding Source={x:Reference cboCustHeader}, Path=DataContext.dpCustomerListOC}"/>
         </CompositeCollection>
     </ComboBox.Resources>                               
     <ComboBox.ItemTemplate>
          <DataTemplate>
              <Grid>
                  <Grid.ColumnDefinitions>
                      <ColumnDefinition SharedSizeGroup="A"/>
                      <ColumnDefinition Width="7"/>
                      <ColumnDefinition SharedSizeGroup="B"/>
                  </Grid.ColumnDefinitions>
                  <Grid.Children>
                     <TextBlock Text="{Binding Path=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                     <TextBlock Text="{Binding Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                  </Grid.Children>
              </Grid>
          </DataTemplate>
     </ComboBox.ItemTemplate>
</ComboBox>

指定来源的第二种方式:

<ComboBox x:Name="cboCustHeader" Grid.IsSharedSizeScope="True">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <ComboBoxItem IsEnabled="False">
               <Border Style="{StaticResource ComboHeaderBorder}">
                   <Grid Style="{StaticResource ComboHeaderStyle}">
                       <Grid.ColumnDefinitions>
                           <ColumnDefinition SharedSizeGroup="A"/>
                           <ColumnDefinition Width="7"/>
                           <ColumnDefinition SharedSizeGroup="B"/>
                       </Grid.ColumnDefinitions>
                       <Grid.Children>
                           <TextBlock Grid.Column="0" Text="ID"/>
                           <TextBlock Grid.Column="2" Text="Name"/>
                       </Grid.Children>
                   </Grid>
               </Border>
           </ComboBoxItem>
           <CollectionContainer Collection="{Binding Path=dpCustomerListOC, Source={StaticResource ReserveVM}}"/>                                                                      
      </CompositeCollection>
  </ComboBox.ItemsSource>
  <ComboBox.ItemTemplate>
      <DataTemplate>
          <Grid>
             <Grid.ColumnDefinitions>
                 <ColumnDefinition SharedSizeGroup="A"/>
                 <ColumnDefinition Width="7"/>
                 <ColumnDefinition SharedSizeGroup="B"/>
            </Grid.ColumnDefinitions>
            <Grid.Children>
                <TextBlock Grid.Column="0" Text="{Binding Path=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                <TextBlock Grid.Column="2" Text="{Binding Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </Grid.Children>
          </Grid>
      </DataTemplate>
  </ComboBox.ItemTemplate>                        

2 个答案:

答案 0 :(得分:0)

您似乎缺少NotifyPropertyChange。使用ComboBoxs时,我通常会做这样的事情:

<ComboBox ItemsSource="{Binding Customers, Mode=OneWay}"
          SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"> </ComboBox>

然后在你的课堂上:

public type Customers
{
    get{return _Customers} 
    set{_SelectedCustomer = value; 
        NotifyPropertyChanged("Customers");}
}

public type SelectedCustomer
{
    get{return _SelectedCustomer;} 
    set{_SelectedCustomer = value;
        NotifyPropertyChanged("SelectedCustomer")}
}

语法可能有些偏差,但是这样的东西应该填充ComboBox。

答案 1 :(得分:0)

在视图模型上添加公开集合的公共属性:

public ObservableCollection<Customer> DpCustomerListOC 
{ 
    get { return dpCustomerListOC; }
}

将组合的ItemsSource绑定到它:

    <ComboBox x:Name="cboCustHeader" Grid.IsSharedSizeScope="True">
        <ComboBox.ItemsSource>
            <CompositeCollection>
                <ComboBoxItem IsEnabled="False">
                    <Border Style="{StaticResource ComboHeaderBorder}">
                        <Grid Style="{StaticResource ComboHeaderStyle}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition SharedSizeGroup="A"/>
                                <ColumnDefinition Width="7"/>
                                <ColumnDefinition SharedSizeGroup="B"/>
                            </Grid.ColumnDefinitions>
                            <Grid.Children>
                                <TextBlock Grid.Column="0" Text="ID"/>
                                <TextBlock Grid.Column="2" Text="Name"/>
                            </Grid.Children>
                        </Grid>
                    </Border>
                </ComboBoxItem>
                <CollectionContainer Collection="{Binding Path=DpCustomerListOC, Source={StaticResource ReserveVM}}"/>
            </CompositeCollection>
        </ComboBox.ItemsSource>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition SharedSizeGroup="A"/>
                        <ColumnDefinition Width="7"/>
                        <ColumnDefinition SharedSizeGroup="B"/>
                    </Grid.ColumnDefinitions>
                    <Grid.Children>
                        <TextBlock Grid.Column="0" Text="{Binding Path=CustomerId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                        <TextBlock Grid.Column="2" Text="{Binding Path=CustomerName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                    </Grid.Children>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

确保在构造函数中填充集合。