WPF DataTrigger绑定不起作用

时间:2015-07-23 14:17:28

标签: checkbox triggers wpfdatagrid

我想在我的应用程序中使用一个函数,如果检查了行中的第一个复选框,则必须启用同一行中的第二个复选框。它必须发生的另一种情况是,如果未选中第一个复选框,则必须禁用第二个复选框。为什么我对第一个复选框的绑定不起作用?

XAML:

{
  "$schema": "http://schemastore.org/schemas/json/webjob-publish-settings.json",
  "webJobName": "MyJob",
  "startTime": "2015-07-21T00:30:00-08:00",
  "endTime": null,
  "jobRecurrenceFrequency": "Day",
  "interval": 1,
  "runMode": "Scheduled"
}

代码:

 <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <DockPanel LastChildFill="True">
            <Button DockPanel.Dock="Bottom" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
            <DataGrid x:Name="dgGrid" ItemsSource="{Binding}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" DockPanel.Dock="Top" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Text" Binding="{Binding Path=Text}" />
                      <DataGridTemplateColumn Header="Var.1">
                       <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <CheckBox x:Name="cb1" IsEnabled="True">
                            </CheckBox>
                        </DataTemplate>
                      </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
             <DataGridTemplateColumn Header="Var2">
              <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox x:Name="cb2">
                        <CheckBox.Style>
                            <Style TargetType="{x:Type CheckBox}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=cb1}" Value="True">
                                        <Setter Property="IsEnabled" Value="True" />
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding ElementName=cb1}" Value="False">
                                        <Setter Property="IsEnabled" Value="False" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </CheckBox.Style>
                    </CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
            </DataGrid>
        </DockPanel>

0 个答案:

没有答案