我有类似下面的xaml。我无法将chkExtract复选框绑定到ChkHeaderExtract。我想为ChkHeaderExtract做一些事情,比如IsChecked =" {Binding ElementName = chkExtract"等等}。但是我无法访问chkExtract名称。此外,我无法从cs文件中的任何位置访问chkExtract控件。任何人都可以帮我解决这个问题。
<DataGrid x:Name="DgEntities" FrozenColumnCount="13" IsReadOnly="True" MaxWidth="854" Height="444" CanUserAddRows="false" SelectedItem="{Binding GetSelectedRow,Mode=TwoWay,NotifyOnTargetUpdated=True}" ItemsSource="{Binding JobEntitiesCollectionViewSource.View, Mode=OneWay, NotifyOnTargetUpdated=True}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" Header="Entity" Width="202" MaxWidth="202" MinWidth="202"/>
<DataGridTemplateColumn Header="Status" Width="138" MaxWidth="138" MinWidth="138">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Margin="5" Content="{Binding ExtractStatus, Mode=TwoWay, NotifyOnTargetUpdated=True}" Command="{Binding DataContext.HyperlinkCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding SelectedItem, ElementName=DgEntities}" Cursor="Hand" MouseDoubleClick="Control_OnMouseDoubleClick" >
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock Style="{StaticResource TxtblkStyleExtract}" TextDecorations="None"><InlineUIContainer>
<ContentPresenter />
</InlineUIContainer></TextBlock>
</ControlTemplate>
</Button.Template>
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="40" MaxWidth="40" MinWidth="40">
<DataGridTemplateColumn.Header>
<CheckBox x:Name="ChkHeaderExtract" Width="26" Command="{Binding DataContext.HeaderExtractCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},Mode=OneWay}" CommandParameter="{Binding IsChecked,RelativeSource={RelativeSource Self}}" IsChecked="{Binding DataContext.ExtractHeaderCheck,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},Mode=TwoWay}" />
</DataGridTemplateColumn.Header>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox x:Name="chkExtract" HorizontalAlignment="Center" VerticalAlignment="Center" Command="{Binding DataContext.IndividualExtractCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}},Mode=OneWay}" CommandParameter="{Binding IsChecked,RelativeSource={RelativeSource Self}}" IsChecked="{Binding ExtractIsSelected, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:0)
问题是chkExtract
在DataTemplate中,这意味着它不知道它之外的名称。
这也意味着在.cs文件中无法看到DataTemplate中指定的项目(代码隐藏)
让复选框相互影响的最佳方法是通过ViewModel;将chkExtract.IsChecked
绑定到网格使用的集合中的对象的属性,然后将标头的ChkHeaderExtract.IsChecked
绑定到DataGrid的ViewModel上的属性。这样,您就可以使用ViewModel中的C#代码控制它们的交互。