无法使用样式触发器禁用WPF DataGrid中的行

时间:2016-01-20 10:10:19

标签: wpf datagrid

我想根据属性" IsEditable"禁用行。

这是我的RowStyle

<Style TargetType="{x:Type DataGridRow}">
    <Setter Property="IsEnabled" Value="True"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsEditable}" Value="False">
             <Setter Property="IsEnabled" Value="False" />
        </DataTrigger>
    </Style.Triggers>
</Style>

我已实施INotifyPropertyChanged

我可以使用相同的属性禁用整个网格,但我无法禁用行。

&#34; IsEditable&#34; property在我的ViewModel中定义。我的datacontext也是ViewModel。

这是我的网格代码

 <DataGrid RowHeaderWidth="0" MouseDoubleClick="listViewItem_MouseDoubleClick"  
                      AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Extended" SelectionUnit="FullRow" 
                      BorderBrush="#FF898C95" ItemsSource="{Binding EqpGrpList}" CanUserSortColumns="False" HorizontalGridLinesBrush="#FFDEDBDB"
                      VerticalGridLinesBrush="#FFDEDBDB" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                      Margin="0,-5,0,0" Background="White" CellStyle="{StaticResource DataGridCellStyle}">

              <DataGrid.Columns...>

  </DataGrid>

我已在UserControl.Resources中添加了行样式。

1 个答案:

答案 0 :(得分:1)

将您的属性绑定到DataGrid ItemCollection类。

... XAML

<Style x:Key="DataGridCellStyle" TargetType="DataGridRow">
            <Setter Property="IsEnabled" Value="True"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsEnable}" Value="False">
                    <Setter Property="IsEnabled" Value="False" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

<DataGrid RowHeaderWidth="0"
                      AutoGenerateColumns="False" IsReadOnly="True" SelectionMode="Extended" SelectionUnit="FullRow" 
                      BorderBrush="#FF898C95" ItemsSource="{Binding GridData}" CanUserSortColumns="False" HorizontalGridLinesBrush="#FFDEDBDB"
                      VerticalGridLinesBrush="#FFDEDBDB" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                      Margin="0,-5,0,0" Background="White" RowStyle="{StaticResource DataGridCellStyle}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" />
                <DataGridTextColumn Header="City" Binding="{Binding City}"/>                
            </DataGrid.Columns>            
        </DataGrid>

...代码

  public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            for(int i=0;i<10;i++)
            {
                GridData.Add(new UserData { Name = "NAME " + i, City = "CITY " + i });
            }
        }
        private ObservableCollection<UserData> _gridData=new ObservableCollection<UserData>();
        public ObservableCollection<UserData> GridData
        {
            get { return _gridData; }
            set { _gridData = value; }
        }

...类

 public class UserData
    {
        public string Name { get; set; }
        public string City { get; set; }

        private bool _isEnable = false;
        public bool IsEnable
        {
            get { return _isEnable; }
            set { _isEnable = value; }
        }
    }