简体   繁体   English

WPF的DataGridComboBoxColumn绑定?

[英]WPF DataGridComboBoxColumn Binding?

I'm trying to include a ComboBox column in a WPF datagrid. 我试图在WPF数据网格中包括一个ComboBox列。 I'm trying to bind this column to an observable collection in the ViewModel however, at run time the cells remain empty. 我试图将此列绑定到ViewModel中的一个可观察的集合,但是,在运行时单元格保持为空。 Datacontext is correct, as all normal columns bind successfully. Datacontext是正确的,因为所有普通列均已成功绑定。 I want to display 'regionShortCode' in the UI. 我想在用户界面中显示“ regionShortCode”。 Here's my xaml: 这是我的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>

& here is my ObservableCollection declared in the ViewModel. 这是我在ViewModel中声明的ObservableCollection。 The Collection is populated successfully from a method invoked in the constructor: 从构造函数中调用的方法成功填充了Collection:

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

At run time I get: 在运行时,我得到:

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')

in the Output window. 在输出窗口中。 Why can't the Xaml resolve this binding? Xaml为什么无法解析此绑定? Thanks 谢谢

If you want to data bind to a single collection property in the view model from the DataGrid , then your answer to Why can't the Xaml resolve this binding? 如果要从DataGrid将数据绑定到视图模型中的单个集合属性,那么您对为什么Xaml无法解析此绑定的答案 is because you didn't tell the Framework where to look for the actual property... you'll need to use a [RelativeSource Binding ] 1 . 这是因为您没有告诉Framework在哪里寻找实际的属性……您需要使用[RelativeSource Binding ] 1 Try this instead: 尝试以下方法:

<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>

With this Binding Path , the Framework will look outside of the normal DataGrid.DataContext for a property named MembershipsCollection in the object that is set as the DataContext of the YourView (clearly this is not the actual name) UserControl or Window . 使用此Binding Path ,框架将在普通DataGrid.DataContext外部查找对象中名为MembershipsCollection的属性,该属性设置为YourViewDataContext (显然这不是实际名称) UserControlWindow If y our view model is correctly set as the DataContext then this should work just fine. 如果y我们的视图模型正确设置为DataContext那么这应该工作正常。

You have to provide exact path to the collection to bind with the itemssource of the combobox. 您必须提供集合的确切路径,才能与组合框的itemssource绑定。 for this make your ancestor as your main control ie window or the usercontrol 为此,使您的祖先成为您的主要控件,即窗口或用户控件

           <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>

And here is the collection of categories i defined in code behind where category is some class you can define your own collection 这是我在代码中定义的类别的集合,其中category是一些类,您可以定义自己的集合

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

A much easier solution with DataGridComboBoxColumn is to set ItemSource binding programmatically in Window class' constructor. 使用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>

in code behind 在后面的代码中

public Win32599087()
        {
            InitializeComponent();

            MemberShipGridNormal.DataContext = myCollection;

            Binding b = new Binding();
            b.Source =  myCollection;
            b.Path = new PropertyPath("collectionpropertyname");

            BindingOperations.SetBinding(CmbMemberships, DataGridComboBoxColumn.ItemsSourceProperty, b);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM