WPF DataGrid在列之间绑定数据 [英] WPF DataGrid bind data between columns

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

问题描述

假设我的数据网格中有2列:列A:选定列和列B:名称。 已选择列是复选框。而名称列是文本字段。如果选中列A的复选框,我想将名称列中的文本的颜色设置为蓝色,否则为红色。



本质上我不知道如何在datagrid的列之间绑定数据。

解决方案

我没有使用 WPF Toolkit的DataGrid ,但是从我可以收集到的方法,一种方法是使用DataGridTemplateColumn,然后设置你的 DataTriggers 基于绑定。



这是一个使用DataTriggers设置前景颜色的样式以及整行背景颜色的示例。值得注意的是,您需要在ItemsSource的绑定中使用布尔属性,以使其能够使用此方法。



XAML

 < Window.Resources> 

< Style TargetType ={x:Type tk:DataGridRow}>
< Style.Triggers>
< DataTrigger
Binding ={Binding Path = IsSelected}Value =True>
< Setter Property =BackgroundValue =Yellow/>
< / DataTrigger>
< /Style.Triggers>
< / Style>

< Style TargetType ={x:Type TextBlock}x:Key =MyTextBlockStyle>
< Style.Triggers>
< DataTrigger
Binding ={Binding Path = IsSelected}Value =True>
< Setter Property =ForegroundValue =Blue/>
< / DataTrigger>
< DataTrigger
Binding ={Binding Path = IsSelected}Value =False>
< Setter Property =ForegroundValue =Red/>
< / DataTrigger>
< /Style.Triggers>
< / Style>


< /Window.Resources>
< Grid>
< tk:DataGrid x:Name =MyGrid
AutoGenerateColumns =False
ItemsSource ={Binding}>
< tk:DataGrid.Columns>

< tk:DataGridTemplateColumn Header =Selected
Width =75>
< tk:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< CheckBox IsChecked ={Binding Path = IsSelected}/>
< / DataTemplate>
< / tk:DataGridTemplateColumn.CellTemplate>
< / tk:DataGridTemplateColumn>

< tk:DataGridTemplateColumn Header =NameWidth =100>
< tk:DataGridTemplateColumn.CellTemplate>
< DataTemplate>
< TextBlock Text ={Binding Path = Name}
Style ={StaticResource MyTextBlockStyle}/>
< / DataTemplate>
< / tk:DataGridTemplateColumn.CellTemplate>
< / tk:DataGridTemplateColumn>

< / tk:DataGrid.Columns>
< / tk:DataGrid>

< / Grid>

背后的代码

  public partial class DataGridDataTrigger:Window 
{
public List< Person>人{get;组; }
public DataGridDataTrigger()
{
InitializeComponent();

var names = new List< string> {Joe,Bob,Frank,Scott,Mike};
People = new List< Person>();
names.ForEach(x => People.Add(new Person {Name = x}));

People.ForEach(x =>
{
if(x.Name.Contains(o))
x.IsSelected = true;
});

MyGrid.DataContext = People;
}
}

public class Person
{
public string Name {get;组; }
public bool IsSelected {get;组; }
}


Lets say I have 2 columns in my data Grid: Column A: Selected, and Column B: Name. The Selected column is a checkbox. And Name column is text field. I want to set the color of the text in 'Name' column as Blue if Column A's check box is checked, and Red otherwise.

Essentially I don't know how to bind data between columns of the datagrid. And sample code/link providing example would be useful.

解决方案

I haven't used the WPF Toolkit's DataGrid much, but from what I can gather, one method is to use DataGridTemplateColumn's and then set up your DataTriggers based on the binding.

Here is an example that uses DataTriggers to set the style of the Foreground color as well the entire row's background color. Of note, you'll need a boolean Property in your ItemsSource's binding to make this work with this method.

XAML

<Window.Resources>

<Style TargetType="{x:Type tk:DataGridRow}">
    <Style.Triggers>
        <DataTrigger 
            Binding="{Binding Path=IsSelected}" Value="True">
            <Setter Property="Background" Value="Yellow" />
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="{x:Type TextBlock}" x:Key="MyTextBlockStyle">
    <Style.Triggers>
        <DataTrigger 
            Binding="{Binding Path=IsSelected}" Value="True">
            <Setter Property="Foreground" Value="Blue" />
        </DataTrigger>
        <DataTrigger 
            Binding="{Binding Path=IsSelected}" Value="False">
            <Setter Property="Foreground" Value="Red" />
        </DataTrigger>
    </Style.Triggers>
</Style>


</Window.Resources>
<Grid>
<tk:DataGrid x:Name="MyGrid" 
             AutoGenerateColumns="False"
             ItemsSource="{Binding}">
    <tk:DataGrid.Columns>

        <tk:DataGridTemplateColumn Header="Selected"
                                   Width="75">
            <tk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                     <CheckBox IsChecked="{Binding Path=IsSelected}"/>
                </DataTemplate>
            </tk:DataGridTemplateColumn.CellTemplate>
        </tk:DataGridTemplateColumn>

        <tk:DataGridTemplateColumn Header="Name" Width="100" >
            <tk:DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Name}" 
                               Style="{StaticResource MyTextBlockStyle}" />
                </DataTemplate>
            </tk:DataGridTemplateColumn.CellTemplate>
        </tk:DataGridTemplateColumn>   

    </tk:DataGrid.Columns>
</tk:DataGrid>

</Grid>

Code Behind

public partial class DataGridDataTrigger : Window
{
    public List<Person> People { get; set; }
    public DataGridDataTrigger()
    {
        InitializeComponent();

        var names = new List<string> { "Joe", "Bob", "Frank", "Scott", "Mike" };
        People = new List<Person>();
        names.ForEach( x => People.Add( new Person { Name = x } ) );

        People.ForEach( x =>
                           {
                               if( x.Name.Contains( "o" ) )
                                   x.IsSelected = true;
                           } );

        MyGrid.DataContext = People;
    }
}

public class Person
{
    public string Name { get; set; }
    public bool IsSelected { get; set; }
}

这篇关于WPF DataGrid在列之间绑定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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