任何人都可以通过以下方式让我感到不舒服
我正在使用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>
编辑: 做了什么blindmeis和威尔建议。我把它作为我当前的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')
答案 0 :(得分:3)
<强>第一强>
我注意到我的属性是在错误的VM(它在Child VM中,这是我的行)修复这个并没有解决我的问题
<强>最后强>
在阅读this article后,我能够解决它
public class BindingProxy : Freezable
{
#region Overrides of Freezable
protected override Freezable CreateInstanceCore()
{
return new BindingProxy();
}
#endregion
public object Data
{
get { return (object)GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
}
<强>资源强>
<DataGrid.Resources>
<local:BindingProxy x:Key="proxy" Data="{Binding}" />
</DataGrid.Resources>
<强>列强>
<DataGridCheckBoxColumn Width="60" Header="Freigabe" CanUserReorder="False"
IsReadOnly="{Binding Data.noCheckPermission, Source={StaticResource proxy}}"
Binding="{Binding FreigegebenL}"/>
答案 1 :(得分:1)
如果您只想让DataGridCheckBoxColumn只读,那么您应该将样式添加到DataGridCheckBoxColumn。
<DataGrid Height="300" AutoGenerateColumns="False" SelectionMode="Single"
x:Name="grd"
ItemsSource="{Binding Itemlist, UpdateSourceTrigger=PropertyChanged}">
<DataGrid.Columns>
<DataGridCheckBoxColumn Width="60"
Header="Freigabe" Binding="{Binding FreigegebenL}" CanUserReorder="False">
<DataGridCheckBoxColumn.Style>
<Style TargetType="{x:Type DataGridCheckBoxColumn}">
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext.noCheckPermission, ElementName=grd}" Value="False">
<Setter Property="IsReadOnly" Value="True"/>
</DataTrigger>
<DataTrigger Binding="{Binding DataContext.noCheckPermission, ElementName=grd}" Value="True">
<Setter Property="IsReadOnly" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.Style>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>