【问题标题】:Disable DataGridRows depending on a bool flag in WPF根据 WPF 中的布尔标志禁用 DataGridRows
【发布时间】:2015-01-17 09:04:56
【问题描述】:

我想灰显并禁用数据网格中的特定行。所有将布尔标志 IsEnabled 设置为 false 的行都应灰显并禁用以进行编辑。我怎样才能做到这一点?

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

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

由于 IsEnabled 为 false,Row2 应灰显并禁用编辑

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

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

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

【问题讨论】:

    标签: c# wpf wpfdatagrid


    【解决方案1】:

    制作 RowStyle 和触发器 IsEnabled 属性 像这样

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-29
      • 1970-01-01
      • 2017-10-10
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2019-04-17
      相关资源
      最近更新 更多