3

任何人都可以启发我以下方法不起作用


在执行我的代码后使用 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')

4

2 回答 2

3

首先
我注意到我的财产在错误的虚拟机中(它在我的行的子虚拟机中)修复这个并没有解决我的问题

终于
在阅读这篇文章后我能够解决它

浓缩形式

班级

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));
}

XAML

资源

<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}"/>
于 2013-04-23T12:18:04.040 回答
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>
于 2013-04-19T07:36:09.800 回答