【问题标题】:Change Color of DataGrid更改 DataGrid 的颜色
【发布时间】:2017-11-15 06:58:27
【问题描述】:

我想用我当前的代码更改整行的颜色,但 row is nulldatagrid.Rows 不存在。

例如,我想突出显示第 3rd 行。

var row = datagrid.ItemContainerGenerator.ContainerFromItem(3) as Microsoft.Windows.Controls.DataGridRow;
row.Background = Brushes.Blue; 

【问题讨论】:

  • 那是 WPF,不是吗?
  • 是的,是.......
  • 背景颜色是否取决于属性或其他内容?您可以尝试使用样式。
  • 它是 Microsoft.Windows.Controls.DataGrid
  • 是的,这取决于。而不是那个 3 它有一个函数并根据计时器更改此值

标签: c# .net wpf datagrid


【解决方案1】:

试试这样的:

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
          <Style.Triggers>
                <DataTrigger Binding="{Binding Executed}" Value="False">
                      <Setter Property="Background" Value="LightCoral" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Executed}" Value="True">
                      <Setter Property="Background" Value="LightGreen" />
                </DataTrigger>
          </Style.Triggers>     
    </Style>
</DataGrid.RowStyle>

在这种情况下,我使用 caliburn micro 来根据行中的布尔值绑定背景颜色(使用布尔值?在布尔值更改之前保持白色)。

【讨论】:

  • 如何触发颜色变化?在代码中。
  • 通过将 bool 设置为 false 或 true。 private bool _executed; public bool Executed { get =&gt; _executed; set { _executed = value; NotifyOfPropertyChange(); } 这就是您使用 caliburn micro 从视图模型中执行此操作的方式。设置执行将更新视图
【解决方案2】:

这并不是改变DataGridRow 背景的最佳方式——您应该按照@David Danielewicz 的建议使用Style——但对于您当前的工作方法,您应该转换从该方法返回的对象到 System.Windows.Controls.DataGridRow.

您还应该使用ContainerFromIndex 方法来获取对第四个元素的可视容器的引用。第三个元素的索引为 2。

试试这个:

var row = datagrid.ItemContainerGenerator.ContainerFromIndex(2) as System.Windows.Controls.DataGridRow;
row.Background = Brushes.Blue;

另请注意,要使其正常工作,您需要等到容器实际创建完成:

datagrid.Loaded += (ss, ee) => 
{
    var row = datagrid.ItemContainerGenerator.ContainerFromIndex(2) as System.Windows.Controls.DataGridRow;
    row.Background = Brushes.Blue;
};

【讨论】:

  • 同样的事情。行 = 空
  • 您实际在哪里执行此代码?您必须等到容器实际创建完毕。
  • 嗯,它说它为空,但代码不会崩溃。如果我检查steps.ItemContainerGenerator,我可以看到所有项目。所以我很确定它正在从表中获取数据。
  • 尝试根据我的编辑在 Loaded 事件处理程序中调用 ContainerFromIndex,如果仍然无法正常工作,请提供问题的完整示例:stackoverflow.com/help/how-to-ask
【解决方案3】:

从后面的代码访问 View 是一种不好的做法。更好地利用 MVVM 的威力:

<Window>
    <Window.Resources>
        <ResourceDictionary>
            <Style x:Key="DataGridRowStyle" TargetType="DataGridRow">
                <Setter Property="Background" Value="{Binding RowBackground}"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <DataGrid ItemsSource="{Binding Records}" RowStyle="{StaticResource DataGridRowStyle}" AutoGenerateColumns="False" CanUserAddRows="False">        
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Value}" Width="*"/>
        </DataGrid.Columns>
    </DataGrid>    
</Window>

public MainWindow()
{
    InitializeComponent();
    DataContext = new MainWindowViewModel(); 
}

public class MainWindowViewModel
{
    public MainWindowViewModel()
    {
        Records.Add(new RecordViewModel()
        {
            Value = "Red",
            RowBackground = new SolidColorBrush(Colors.LightCoral)
        });

        Records.Add(new RecordViewModel()
        {
            Value = "Green",
            RowBackground = new SolidColorBrush(Colors.LightGreen)
        });

        Records.Add(new RecordViewModel()
        {
            Value = "Blue",
            RowBackground = new SolidColorBrush(Colors.LightBlue)
        });

        Records[2].Value = "Not blue anymore";
        Records[2].RowBackground = new SolidColorBrush(Colors.LightPink);
    }

    public ObservableCollection<RecordViewModel> Records { get; } = new ObservableCollection<RecordViewModel>();
}

public class RecordViewModel : INotifyPropertyChanged
{
    private string _value;
    private Brush _rowBG;
    public event PropertyChangedEventHandler PropertyChanged;

    public string Value
    {
        get
        {
            return _value;
        }
        set
        {
            _value = value;
            OnPropertyChanged(nameof(Value));
        }
    }

    public Brush RowBackground
    {
        get
        {
            return _rowBG;
        }
        set
        {
            _rowBG = value;
            OnPropertyChanged(nameof(RowBackground));
        }
    }

    private void OnPropertyChanged(string name)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
    }
}

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-04
    • 2021-04-06
    • 2012-01-01
    • 2019-02-16
    相关资源
    最近更新 更多