简体   繁体   English

DataGridComboBoxColumn绑定

[英]DataGridComboBoxColumn binding

I have seen a few posts on here with people getting confused on how to bind to a DataGridComboBoxColumn, 我在这里看到了一些帖子,人们对如何绑定到DataGridComboBoxColumn感到困惑,

I have 我有

<DataGridComboBoxColumn SelectedItemBinding="{Binding Collection}"  DisplayMemberPath="Name" Header="Name" Width="70">

which didnt work.. 没用..

So I used 所以我用

<DataGridComboBoxColumn ItemBinding="{Binding Collection}" DisplayMemberPath="Name"> Header="Name" Width="70">

Which again didn't work, why is binding to a datagridcombo different to a original combo box. 这又一次不起作用,为什么绑定到与原始组合框不同的datagridcombo。

<ComboBox ItemsSource="{Binding Collection}" DisplayMemberPath="Name" HorizontalAlignment="Left">

which does work 哪个有效

What is the correct method of binding to a combo box inside a DataGrid? 绑定到DataGrid内部组合框的正确方法是什么?

---Edit--- - -编辑 - -

I might have found the problem, I have a DataGrid binding to a ItemSource, however, I want the ComboBoxColumn to be bounded to a different Itemsource, is this possible? 我可能已经发现问题了,我有一个DataGrid绑定到ItemSource,但是,我希望将ComboBoxColumn绑定到另一个ItemSource,这可能吗?

Cheers 干杯

You need to bind to ItemsSource property. 您需要绑定到ItemsSource属性。 Set it in EditingElementStyle . EditingElementStyle设置。

<DataGridComboBoxColumn>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource" Value="{Binding Collection}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>

In case you want ItemsSource to bind to collection which is outside of DataGrid underlying source object, you can do that as well. 如果希望ItemsSource绑定到DataGrid基础源对象之外的集合,也可以执行此操作。

Say you have collection AnotherCollection residing in ViewModel of Window/UserControl, you can bind with it using RelativeSource markup extension. 假设您在Window / UserControl的ViewModel中驻留了AnotherCollection集合,则可以使用RelativeSource标记扩展与其绑定。

Also, you have to set SelectedItemBinding to the property where you want to set the value selected from ComboBox and declare same style under ElementStyle of DataGridComboBoxColumn. 另外,还必须将SelectedItemBinding设置为要设置从ComboBox中选择的值的属性,并在DataGridComboBoxColumn的ElementStyle下声明相同的样式。 Suppose property name is Name to which you want to bind. 假设属性名称是您要绑定的Name

<DataGridComboBoxColumn SelectedItemBinding="{Binding Name}">
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource"
                    Value="{Binding DataContext.AnotherCollection,
                               RelativeSource={RelativeSource Mode=FindAncestor, 
                                                         AncestorType=Window}}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
    <DataGridComboBoxColumn.ElementStyle>
        <Style TargetType="ComboBox">
            <Setter Property="ItemsSource"
                    Value="{Binding DataContext.AnotherCollection,
                              RelativeSource={RelativeSource Mode=FindAncestor, 
                                                         AncestorType=Window}}"/>
            <Setter Property="DisplayMemberPath" Value="Name"/>
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
</DataGridComboBoxColumn>

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

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