簡體   English   中英

WPF DataGrid - 如何設置正確的 DataTrigger 綁定到單元格的數據源(而不是行的源)

[英]WPF DataGrid - How to setup correct DataTrigger binding to cell's data source (and not row's source)

嘗試根據 WPF DataGrid 中的單元格對象屬性設置單元格的背景,我收到一個錯誤,即找不到該屬性(但在行對象上):

System.Windows.Data 錯誤:40:BindingExpression 路徑錯誤:在“對象”“MyRow”(HashCode = 48826322)上找不到“IsOn”屬性。 綁定表達式:路徑=IsOn; DataItem='MyRow' (HashCode=48826322); 目標元素是'DataGridCell'(名稱=''); 目標屬性是“NoTarget”(類型“對象”)

我想知道,為什么 DataTrigger 綁定正在處理 object“MyRow”行,因為 DataTrigger 是為 CellStyle 定義的/在 CellStyle 內部定義的。

XAML:

<DataGrid Name="tblTest" Grid.Column="2" IsReadOnly="True" AutoGenerateColumns="True">
    <DataGrid.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Background" Value="PaleGreen" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOn}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

C#

class MyCell
{
    public MyCell( string v)
    {
        Value = v;
    }
    public string Value { get; set; }
    public bool IsOn { get => Value == "one";  }
    public override string ToString()
    {
        return Value;
    }
}

class MyRow
{
    public MyCell One { get; set;  }
    public MyCell Two { get; set;  }
}

void SetupTestTable()
{
    List<MyRow> data = new();
    data.Add(new MyRow
    {
        One = new MyCell("one"),
        Two = new MyCell("two")
    });
    tblTest.ItemsSource = data;
}

在此處輸入圖像描述

那么如何正確綁定單元格 object “MyCell”?

DataGridCells 與 DataGridRow 具有相同的 DataContext - 以通用方式做不同的事情有很多障礙。 所以單個 DataGrid.CellStyle 不起作用

我將使用AutoGeneratingColumn為每一列創建單元格 styles。 但是,它們將基於存儲在 DataGrid.Resources 中的現有樣式。

<DataGrid Name="tblTest" Grid.Column="2" IsReadOnly="True" 
          AutoGenerateColumns="True"
          AutoGeneratingColumn="tblTest_AutoGeneratingColumn">
    <DataGrid.Resources>
        <Style TargetType="{x:Type DataGridCell}" x:Key="ColoredCellStyle">
            <Setter Property="Background" Value="Cyan" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Tag.IsOn, RelativeSource={RelativeSource Self}}" Value="True">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
</DataGrid>

我正在使用綁定到 Tag 而不是 DataContext,因為 DataContext 是MyRow object。 Tag中會有MyCell對象。 它是在事件處理程序中實現的:

private void tblTest_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.Column is DataGridTextColumn tc && tc.Binding is Binding binding)
    {
        // unique value for each column
        var property = binding.Path.Path;

        // DataGrid reference to get Resources
        var dg = (DataGrid)sender;

        // new cell style which inherits trigger from ColoredCellStyle and binds Tag to MyCell property
        var cellStyle = new Style
        {
            TargetType = typeof(DataGridCell),
            BasedOn = (Style)dg.Resources["ColoredCellStyle"],
            Setters =
            {
                new Setter
                { 
                    Property = DataGridCell.TagProperty, 
                    Value = new Binding(property)
                }
            }
        };

        tc.CellStyle = cellStyle;
    };
}

彩色細胞

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM