WPF DataGrid触发其他单元格值

时间:2017-02-27 09:54:03

标签: c# wpf datagrid

我有DataGrid

<DataGrid x:Name="PART_DataGrid"
            ItemsSource="{TemplateBinding Items}"
            AutoGenerateColumns="False"
            Margin="2,25,2,2">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
                            ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
                            FontSize="16"
                            IsEnabled="False">
                    <ComboBox.Style>
                        <Style TargetType="ComboBox">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=test }" Value="">
                                    <Setter Property="IsEnabled" Value="True"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ComboBox.Style>
                </ComboBox>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn  Header="Translation" Width="SizeToCells" IsReadOnly="False">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox x:Name="TranslationValueTB" 
                    Text="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}"
                            BorderThickness="0"
                            FontSize="16" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

我想设置当第二个ComboBox为空时将启用第一列TextBox.Text,我尝试了不同的绑定方法,但没有一个有效。那我怎么能这样做呢?

2 个答案:

答案 0 :(得分:1)

绑定到Value源属性,即第二列中的TextBox绑定到:

<DataGridTemplateColumn Header="Language" Width="SizeToCells" IsReadOnly="False">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox SelectedItem="{Binding Path=Language, Mode=OneWay}"
                                ItemsSource="{Binding LanguagesSource, RelativeSource={RelativeSource AncestorType=UserControl}}"
                                FontSize="16"
                                IsEnabled="False">
                <ComboBox.Style>
                    <Style TargetType="ComboBox">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Value}" Value="{x:Null}">
                                <Setter Property="IsEnabled" Value="True"/>
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Value}" Value="">
                                <Setter Property="IsEnabled" Value="True"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox.Style>
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

还要确保定义Value属性的类实现INotifyPropertyChanged接口,并在PropertyChanged属性的setter中引发Value事件。

答案 1 :(得分:0)

从组合框中删除IsEnabled = false,并在setter中执行相同的操作,如下所示,并执行如下所示的绑定,

<ComboBox.Style>
                                    <Style TargetType="ComboBox">
                                        <Setter Property="IsEnabled" Value="False"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=Item.Name }" Value="{x:Static sys:String.Empty}">
                                                <Setter Property="IsEnabled" Value="True"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </ComboBox.Style>

Item.Name - &gt;这是您需要检查的属性