根据WPF中的bool标志禁用DataGridRows [英] Disable DataGridRows depending on a bool flag in WPF

查看:37
本文介绍了根据WPF中的bool标志禁用DataGridRows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想变灰并禁用datagrid中的特定行.布尔标志IsEnabled设置为false的所有行均应变灰并禁用以进行编辑.我该如何实现?

I would like to gray out and disable specific rows in datagrid. All rows which have the bool flag IsEnabled set to false should be grayed out and disabled for editing. How can I achieve that?

下面您可以找到我的示例代码:

Below you can find my Example Code:

MainWindow.xaml

<Window x:Class="WpfApplicationDisableRows.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplicationDisableRows"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
</Window.Resources>
<StackPanel>
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Width="500" Height="250">
        <DataGrid.Columns>
            <DataGridTextColumn FontWeight="Bold" IsReadOnly="True" Width="200" Header="Description" Binding="{Binding Path=Description}"></DataGridTextColumn>
            <DataGridTextColumn FontWeight="Bold" Width="200" Header="Value" Binding="{Binding Path=Value}"></DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Windows;
using System.Collections.ObjectModel;

namespace WpfApplicationDisableRows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Row> Rows;
        public MainWindow()
        {
            Rows = new ObservableCollection<Row>();
            AddRows();           
            InitializeComponent();
            this.DataContext = Rows;

        }
        public void AddRows()
        {
            Rows.Add(new Row { Description = "Row1", Value = "A", IsEnabled = true });
            Rows.Add(new Row { Description = "Row2", Value = "B", IsEnabled = false });
            Rows.Add(new Row { Description = "Row3", Value = "C", IsEnabled = true });
        }
    }
}

Row.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WpfApplicationDisableRows
{
    public class Row : INotifyPropertyChanged
    {
        private bool _IsEnabled;
         public event PropertyChangedEventHandler PropertyChanged;
        public string Description { get; set; }
        public string Value { get; set; }
        public bool IsEnabled
        {
            get
            {
                return _IsEnabled;
            }
            set
            {
                _IsEnabled = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IsEnabled"));
            }
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }  
    }
}

Row2应该变灰并禁止编辑,因为IsEnabled为false

Row2 should be grayed out and disabled for editing because IsEnabled is false

Description = "Row2", Value = "B", IsEnabled = false

Row1和Row3具有标志IsEnabled = true,这些行应该是可编辑的.

Row1 and Row3 have the flag IsEnabled=true, these rows should be editable.

Description = "Row1", Value = "A", IsEnabled = true
Description = "Row3", Value = "C", IsEnabled = true

推荐答案

制作RowStyle和Trigger IsEnabled属性像这样

Make RowStyle and Trigger IsEnabled Property Like this

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

这篇关于根据WPF中的bool标志禁用DataGridRows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆