WPF DataGrid DataTrigger绑定DataContext属性 [英] WPF DataGrid DataTrigger Binding on DataContext property

查看:996
本文介绍了WPF DataGrid DataTrigger绑定DataContext属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataContext属性的DataTrigger绑定是什么?
我有一个这样绑定的DataGrid:



XAML:

 code>< DataGrid x:Name =dataGridGrid.Row =4Grid.ColumnSpan =2
ItemsSource ={Binding}
CellStyle ={StaticResource RowStateStyle }
>
< / DataGrid> code

在.cs中,网格绑定到一个DataTable,因此单元格的DataContext是DataRowView,其中包含作为属性行:

  //包含大量行/列的DataTable 
dataGrid.DataContext = dataTable;

编辑:



参考ASh的解决方案,我编辑了风格,并将其置于不变和修改的触发器中:

 < Style x:Key =RowStateStyleTargetType ={x:Type DataGridCell}> 
< Style.Triggers>
< DataTrigger Binding ={Binding Path = DataContext.Row.RowState,
RelativeSource = {RelativeSource Self}}Value =Unchanged>
< Setter Property =ForegroundValue =Green/>
< / DataTrigger>
< DataTrigger Binding ={Binding Path = DataContext.Row.RowState,
RelativeSource = {RelativeSource Self}}Value =Modified>
< Setter Property =ForegroundValue =Yellow/>
< / DataTrigger>
< DataTrigger Binding ={Binding Path = Content.Text,
RelativeSource = {RelativeSource Self}}Value =test>
< Setter Property =ForegroundValue =Red/>
< / DataTrigger>
< /Style.Triggers>
< / Style>

对Content.Text的触发效果非常好,不变也是如此。但是当我修改单元格(因此DataRowState = Modified)时,没有任何反应,颜色保持绿色。任何解决方案?

解决方案

DataTrigger适用于我,如果我


  1. 使用 DataContext.Row.RowState 路径


  2. 使用 Mode = TwoWay


  3. 并删除枚举名称 DataRowState 当设置值






 < DataTrigger Binding ={Binding Path = DataContext.Row.RowState,
RelativeSource = {RelativeSource Self}}
Value =Unchanged>
< Setter Property =ForegroundValue =Red/>
< / DataTrigger>






我发现另一篇与此问题相关的帖子



WpfToolkit DataGrid:突出显示已修改行



没有通知关于 RowState 更改的默认机制。但是可以在派生的dataTable类中创建一个:

  public class DataTableExt:DataTable 
{
protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
{
返回新的DataRowExt(builder);
}

protected override void OnRowChanged(DataRowChangeEventArgs e)
{
base.OnRowChanged(e);
//行已更改,通知有关更改
var r = e.Row as DataRowExt;
if(r!= null)
r.OnRowStateChanged();
}
}

与派生的dataRow类:

  public class DataRowExt:DataRow,INotifyPropertyChanged 
{
protected internal DataRowExt(DataRowBuilder builder):base(builder)
{
}

internal void OnRowStateChanged()
{
OnPropertyChanged(RowState);
}

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if(handler!= null)handler(this,new PropertyChangedEventArgs(propertyName));
}
}


What's the correct DataTrigger binding for DataContext properties? I have a DataGrid which is bound like this:

XAML:

<DataGrid x:Name="dataGrid" Grid.Row="4" Grid.ColumnSpan="2"
    ItemsSource="{Binding}"
    CellStyle="{StaticResource RowStateStyle}"
>
</DataGrid>

In .cs the grid is bound to a DataTable, hence the DataContext for a cell is DataRowView, containing Row as a property:

// DataTable containing lots of rows/columns
dataGrid.DataContext = dataTable; 

Edit:

Refering to ASh's solution I edited the style and put in Triggers for Unchanged and Modified:

    <Style x:Key="RowStateStyle" TargetType="{x:Type DataGridCell}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=DataContext.Row.RowState,
                RelativeSource={RelativeSource Self}}" Value="Unchanged">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=DataContext.Row.RowState,
                RelativeSource={RelativeSource Self}}" Value="Modified">
                <Setter Property="Foreground" Value="Yellow" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Content.Text,
                RelativeSource={RelativeSource Self}}" Value="test">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

Triggering on Content.Text works perfectly fine, so does Unchanged. However when I modify a cell (thus DataRowState = Modified), nothing happens and the color stays green. Any solution?

解决方案

DataTrigger works for me, if I

  1. use DataContext.Row.RowState path

  2. don't use Mode=TwoWay

  3. and remove enum name DataRowState when set Value

<DataTrigger Binding="{Binding Path=DataContext.Row.RowState,
             RelativeSource={RelativeSource Self}}" 
             Value="Unchanged">
    <Setter Property="Foreground" Value="Red" />
</DataTrigger>


I have found another post related to this issue

WpfToolkit DataGrid: Highlight modified rows

There is no default mechanism which notifies about RowState changes. But it is possible to create one in a derived dataTable class:

public class DataTableExt: DataTable
{
    protected override DataRow NewRowFromBuilder(DataRowBuilder builder)
    {
        return new DataRowExt(builder);
    }

    protected override void OnRowChanged(DataRowChangeEventArgs e)
    {
        base.OnRowChanged(e);
        // row has changed, notifying about changes
        var r = e.Row as DataRowExt;
        if (r!= null)
            r.OnRowStateChanged();
    }
}

with a derived dataRow class:

public class DataRowExt: DataRow, INotifyPropertyChanged
{
    protected internal DataRowExt(DataRowBuilder builder) : base(builder)
    {
    }

    internal void OnRowStateChanged()
    {
        OnPropertyChanged("RowState");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

这篇关于WPF DataGrid DataTrigger绑定DataContext属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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