【发布时间】:2018-01-17 10:29:29
【问题描述】:
我正在创建一个 wpf 应用程序并在一段时间内努力解决这个问题。 我有一个带有 DataGridTemplateColumn 的数据网格,其中包含一个复选框和文本块。
<DataGrid
Name="ChargeDataGrid"
Grid.Row="1"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox x:Name="CheckBox1"/>
<TextBlock Text="{Binding}" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox />
<TextBlock Text="Title" />
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
<system:String>123</system:String>
<system:String>124</system:String>
<system:String>125</system:String>
<system:String>126</system:String>
<system:String>127</system:String>
</DataGrid>
我需要实现的是,当单击该行时,该行中的复选框也必须处于选中状态。 我尝试使用样式触发器:
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="CheckBox1.IsChecked" Value="True" />
<Setter Property="Background" Value="Blue" />
</Trigger>
</Style.Triggers>
</Style>
但似乎无法像这样更改复选框状态。我知道如何以代码隐藏或 mvvm 样式执行此操作,但在这种情况下,我想知道是否可以仅使用 xaml?
任何帮助将不胜感激。
【问题讨论】: