【问题标题】:WPF DataGridComboBoxColumn Binding?WPF DataGridComboBoxColumn 绑定?
【发布时间】:2014-09-03 12:12:37
【问题描述】:

我正在尝试在 WPF 数据网格中包含一个 ComboBox 列。我正在尝试将此列绑定到 ViewModel 中的可观察集合,但是,在运行时单元格保持为空。数据上下文是正确的,因为所有普通列都成功绑定。我想在 UI 中显示“regionShortCode”。这是我的 xaml:

   <DataGridComboBoxColumn Header="Region" DisplayMemberPath="regionShortCode" Width="SizeToHeader">
       <DataGridComboBoxColumn.ElementStyle>
           <Style>
             <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
             </Style>
                </DataGridComboBoxColumn.ElementStyle>
                 <DataGridComboBoxColumn.EditingElementStyle>
              <Style>
                 <Setter Property="ComboBox.ItemsSource" Value="{Binding Path=MembershipsCollection}" />
              </Style>
           </DataGridComboBoxColumn.EditingElementStyle>
     </DataGridComboBoxColumn>

& 这是我在 ViewModel 中声明的 ObservableCollection。集合已从构造函数中调用的方法成功填充:

private ObservableCollection<tbAccountMembership> _MembershipsCollection;
    public ObservableCollection<tbAccountMembership> MembershipsCollection
    {
        get { return _MembershipsCollection; }
        set
        {
            _MembershipsCollection = value;
            OnPropertyChanged("MembershipsCollection");
        }
    }     

在运行时我得到:

System.Windows.Data Error: 40 : BindingExpression path error: 'MembershipsCollection' property not found on 'object' ''tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020)'. BindingExpression:Path=MembershipsCollection; DataItem='tbAccountMembership_041E43AFC29975F12C156BA1373ACD47FC07BBE55614E5AF8AD3BBD9F090C133' (HashCode=46247020); target element is 'TextBlockComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

在“输出”窗口中。为什么 Xaml 无法解析此绑定?谢谢

【问题讨论】:

  • 我正在尝试将此列绑定到 ViewModel 中的可观察集合...对吗?您想将数据绑定到视图模型中的单个集合属性,而不是数据项类中的一个吗?如果是这样,那么您对 ​​Why can't the Xaml resolve this binding? 的回答是因为您没有告诉框架在哪里查找实际属性...您需要使用RelativeSource Binding.
  • 我想绑定到 ObservableCollection 中的单个元素
  • ObservableCollection&lt;T&gt; 声明在哪里?
  • 在 ViewModel 中作为公共属性。问题已更新。

标签: c# wpf xaml mvvm


【解决方案1】:

如果您想从DataGrid 将数据绑定到视图模型中的单个集合属性,那么您对为什么 Xaml 不能解析此绑定?的回答是因为您没有t 告诉框架在哪里查找实际属性...您需要使用 [RelativeSource Binding]1。试试这个:

<DataGridComboBoxColumn Header="Region" DisplayMemberPath="regionShortCode" 
    Width="SizeToHeader">
    <DataGridComboBoxColumn.ElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding 
                DataContext.MembershipsCollection, RelativeSource={RelativeSource 
                AncestorType={x:Type YourViewModelsPrefix:YourViewModel}}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding 
                DataContext.MembershipsCollection, RelativeSource={RelativeSource 
                AncestorType={x:Type YourViewsPrefix:YourView}}}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

有了这个Binding Path,框架将在普通DataGrid.DataContext之外查找对象中名为MembershipsCollection的属性,该属性设置为YourViewDataContext(显然这不是实际的)名称)UserControlWindow。如果我们的视图模型正确设置为DataContext,那么这应该可以正常工作。

【讨论】:

    【解决方案2】:

    您必须提供集合的确切路径才能与组合框的 itemssource 绑定。为此,将您的祖先作为您的主要控件,即窗口或用户控件

               <DataGridComboBoxColumn Header="Category"
    SelectedValueBinding="{Binding category_cde}"
      SelectedValuePath="category_cde"
    DisplayMemberPath="category_dsc">
                            <DataGridComboBoxColumn.ElementStyle>
                                <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=CATEGORIES , RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                </Style>
                            </DataGridComboBoxColumn.ElementStyle>
                            <DataGridComboBoxColumn.EditingElementStyle>
                                <Style TargetType="ComboBox">
                                <Setter Property="ItemsSource" Value="{Binding Path=CATEGORIES , RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                </Style>
                            </DataGridComboBoxColumn.EditingElementStyle>
                        </DataGridComboBoxColumn>
    
                </DataGrid.Columns>
            </DataGrid>
    

    这是我在代码中定义的类别集合,其中类别是您可以定义自己的集合的类

     List<Category> m_categories = new List<Category>();
        public List<Category> CATEGORIES
        {
            get { return m_categories; }
            set { m_categories = value; }
        }
    

    【讨论】:

      【解决方案3】:

      使用 DataGridComboBoxColumn 的一个更简单的解决方案是在 Window 类的构造函数中以编程方式设置 ItemSource 绑定。

      <DataGrid x:Name="MembershipGridNormal" AutoGenerateColumns="False" SelectedIndex="0" ItemsSource="{Binding}">
                  <DataGrid.Columns>
      
                      <DataGridTextColumn Header="Name" Binding="{Binding Prop1}"/>
                      <DataGridTextColumn Header="Value" Binding="{Binding Prop2}"/>
                      <DataGridComboBoxColumn x:Name="CmbMemberShips" Header="RawValues" DisplayMemberPath="Name" />
      
                  </DataGrid.Columns>
              </DataGrid>
      

      在代码后面

      public Win32599087()
              {
                  InitializeComponent();
      
                  MemberShipGridNormal.DataContext = myCollection;
      
                  Binding b = new Binding();
                  b.Source =  myCollection;
                  b.Path = new PropertyPath("collectionpropertyname");
      
                  BindingOperations.SetBinding(CmbMemberships, DataGridComboBoxColumn.ItemsSourceProperty, b);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-25
        • 2011-07-27
        • 1970-01-01
        • 2012-10-28
        • 2017-08-05
        • 2018-01-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多