WPF:将一个DataGridTemplateColumn中的元素绑定到另一个DataGridTemplateColumn中的元素 [英] WPF: Binding an Element in a DataGridTemplateColumn to an Element in another DataGridTemplateColumn

查看:68
本文介绍了WPF:将一个DataGridTemplateColumn中的元素绑定到另一个DataGridTemplateColumn中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

:)我有一个DataGrid.在Column2中有"ComboBox1",在Column3中有"ComboBox2".我想在"ComboBox2"上将IsEnabled设置为false,只要"ComboBox1"的SelectedIndex = 0.分别为每一行.

:) I have one DataGrid. In Column2 there is "ComboBox1", in Column3 there is "ComboBox2". I would like to set IsEnabled to false on "ComboBox2" as long as "ComboBox1" has SelectedIndex=0. For each row seperately.

这可以在Datagrid外部使用两个ComboBox(在Style和DataTrigger的帮助下)完美地工作.但是,在Datagrid内部,在Column3中,我无法看到""ComboBox1"(找不到绑定源...".)

This works perfectly outside the Datagrid with two ComboBoxes (with the help of Style and DataTrigger). However, inside the Datagrid, within Column3 I cannot "see" "ComboBox1" ("Cannot find source for binding...").

这基本上是一个名称范围问题.但是,首先通过名称引用DataGrid中的组合框似乎是错误的.那么:有什么想法可以做到这一点吗?

It's basically a namescope issue. However, referring to a Combobox within a DataGrid by name seems to be wrong in the first place. So: any ideas how to accomplish this?

非常感谢您!

<Window.Resources>
    <CollectionViewSource x:Key="Source1" Source="{Binding List1}" />
    <CollectionViewSource x:Key="Source2" Source="{Binding List2}" />
</Window.Resources>

<DataGrid x:Name="ModelControl" AutoGenerateColumns="False" ItemsSource="{Binding List3}">
    <DataGrid.Columns>
         <DataGridTextColumn Binding="{Binding MyName}" Header="Modellname" />
        <DataGridTemplateColumn Header="Header 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="ComboBox1" DisplayMemberPath="MyName" SelectedIndex="0">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource Source2}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Header 2">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="ComboBox2" DisplayMemberPath="MyName">
                        <ComboBox.Style>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="IsEnabled" Value="True" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding SelectedIndex,ElementName=ComboBox1}" Value="0">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ComboBox.Style>
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource Source1}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

请不要介意CompositeCollection.

Please don't mind the CompositeCollection.

推荐答案

但是,首先在名称上引用DataGrid中的组合框似乎是错误的.那么:有什么想法可以做到这一点吗?

However, referring to a Combobox within a DataGrid by name seems to be wrong in the first place. So: any ideas how to accomplish this?

您应该将SelectedIndex属性绑定到模型类的源属性:

You should bind the SelectedIndex property to a source property of your model class:

<DataGrid x:Name="ModelControl" AutoGenerateColumns="False" ItemsSource="{Binding List3}">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding MyName}" Header="Modellname" />
        <DataGridTemplateColumn Header="Header 1">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="ComboBox1" DisplayMemberPath="MyName" 
                                      SelectedIndex="{Binding YourIndexProperty}">
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource Source2}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTemplateColumn Header="Header 2">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <ComboBox x:Name="ComboBox2" DisplayMemberPath="MyName">
                        <ComboBox.Style>
                            <Style TargetType="{x:Type ComboBox}">
                                <Setter Property="IsEnabled" Value="True" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding YourIndexProperty}" Value="0">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ComboBox.Style>
                        <ComboBox.ItemsSource>
                            <CompositeCollection>
                                <CollectionContainer Collection="{Binding Source={StaticResource Source1}}" />
                            </CompositeCollection>
                        </ComboBox.ItemsSource>
                    </ComboBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

确保模型类型实现INotifyPropertyChanged接口并在设置器中引发PropertyChanged事件:

Make sure that the model type implements the INotifyPropertyChanged interface and raise the PropertyChanged event in the setter: https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v=vs.110).aspx

这篇关于WPF:将一个DataGridTemplateColumn中的元素绑定到另一个DataGridTemplateColumn中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆