任何人都可以启发我以下方法不起作用
我
在执行我的代码后使用 mvvm 模式我仍然能够检查和取消选中我的 DataGridCheckBoxColumn 中的复选框
属性(我的 ViewModel 的一部分)
public bool noCheckPermission
{
get { return false; } // just as example
}
Xaml(我的视图后面没有代码)
<DataGrid Height="300" AutoGenerateColumns="False" SelectionMode="Single"
ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridCheckBoxColumn}">
<Style.Triggers>
<DataTrigger Binding="{Binding noCheckPermission}" Value="False">
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding noCheckPermission}" Value="True">
<Setter Property="IsReadOnly" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="60"
Header="Freigabe" Binding="{Binding FreigegebenL}" CanUserReorder="False">
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
编辑:在做了盲人和威尔建议的事情之后。我把它作为我当前的 XAML
<DataGrid Height="300" Margin="12" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"
CanUserResizeColumns="False" CanUserResizeRows="False" CanUserReorderColumns="False"
x:Name="grd" ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding selectedItem, UpdateSourceTrigger=PropertyChanged}" SelectionMode="Single" SelectionChanged="DataGrid_SelectionChanged" TabIndex="2">
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="60" IsReadOnly="{Binding DataContext, Path=noCheckPermission, ElementName=grd}" Binding="{Binding FreigegebenL}"
Header="Freigabe" CanUserReorder="False">
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
这是我的输出窗口中的错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=noCheckPermission; DataItem=null; target element is 'DataGridCheckBoxColumn' (HashCode=35155182); target property is 'IsReadOnly' (type 'Boolean')