动态绑定在WPF DataGridCell模板 [英] Dynamic Binding in WPF DataGridCell Template

查看:302
本文介绍了动态绑定在WPF DataGridCell模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于数据绑定WPF DataGrid的问题。我现在用的是VS 2010 Beta 2中有其自身的DataGrid,而不是工具包之一,虽然我认为这是pretty大同小异。

I have a question about data binding DataGrid in WPF. I am using the VS 2010 Beta 2 which has its own DataGrid, not the Toolkit one, although I think it is pretty much the same.

我要绑定到其中有52列,一个是一年中的每一周的数据集。为此,我希望将数据绑定动态地而不是指定每个字段。每个字段的值取决于某个条件是真的还是假的。在此基础上的价值我想显示在单元格模板图像,如果条件为真,并隐藏它,如果条件是不正确的。

I want to bind to a dataset which has 52 columns, one for every week of the year. For this reason I want to bind the data dynamically rather than specifying each field. The value for each field is true or false depending on some condition. Based on this value I want to show an image in the cell template if the condition is true and hide it if the condition is not true.

我的问题是,使用,我发现模板的所有例子涉及固定,predefined领域,在那里你可以有一个像文字的绑定={绑定用户名}的情况。这是没有好到我,因为我不知道该字段名称将在设计时的东西。

My problem is that all the examples of using templates that I have found refer to the case of fixed, predefined fields, where you can have a binding like Text ="{Binding UserName}". This is no good to me because I don't know what the field names will be at design time.

我已经决定了它说明了问题的简化示例。在本实施例中产生一个数据表,它包含真假值。在我的模板的形象是不可见。如何将取决于数据的true或false值使它看不见?

I have made up a simplified example which illustrates the problem. In this example a data table is generated which contains true and false values. The Image in my template is never visible. How would I make it invisible depending on the true or false value in the data?

<Window.Resources>

    <!--This is the bit that doesn't work...-->
    <Style TargetType="{x:Type Image}" x:Key="HideWhenFalse">
        <Setter Property="Visibility" Value="Hidden" />
        <Style.Triggers>
            <DataTrigger
        Binding="{Binding Path=???}" 
        Value="True"> <!--What to put for the path? -->
                <Setter Property="Visibility">
                    <Setter.Value>
                        Visible
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <!--Up to here--> 

    <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel>
                        <Image Source="Images/tick.bmp" Style="{StaticResource HideWhenFalse}">

                        </Image>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

<Grid>
    <DataGrid 
        x:Name="myDataGrid"
        AutoGenerateColumns="True" >

    </DataGrid>
</Grid>

背后code:

公共部分类主窗口:窗口
{

public partial class MainWindow : Window {

public MainWindow()
{
    InitializeComponent();

    DataTable dtTable = new DataTable();

    dtTable.Columns.Add("A", typeof(Boolean));
    dtTable.Columns.Add("B", typeof(Boolean));
    dtTable.Columns.Add("C", typeof(Boolean));
    dtTable.Columns.Add("D", typeof(Boolean));
    dtTable.Columns.Add("E", typeof(Boolean));
    dtTable.Columns.Add("F", typeof(Boolean));

    for (int i = 0; i < 5; i++)
    {
        object[] oValues = new Object[dtTable.Columns.Count];

        for (int j = 0; j < dtTable.Columns.Count; j++)
        {
            oValues[j] = (j % 2 == 1) ? true : false;
        }

        dtTable.Rows.Add(oValues);
    }

    myDataGrid.ItemsSource = dtTable.DefaultView;
    myDataGrid.Items.Refresh();
}

}

NB这可能是显而易见的,我在完全错误的方式接近问题。这里是一个自白:我一直在努力,现在让我周围的WPF头几个月,我似乎仍然觉得自己接近每一个问题的错误的方式。我希望很快一分钱下降。

NB This is probably obvious and I am approaching the problem in completely the wrong way. Here is a confession: I have been trying to get my head around WPF for a couple of months now and I still seem to find myself approaching EVERY problem the wrong way. I hope the penny drops soon.

推荐答案

您可以使用 MultiBinding ,与第一结合在实际数据上下文从小区(即将是该行),而第二个考虑的列。从那里,你可以检索单元格的值。

You can use a MultiBinding, with a first binding taking the actual data context from the cell (that would be the row), and the second one taking the column. From there, you can retrieve the cell value.

转换器code:

public class RowColumnToCellConverter : IMultiValueConverter {
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) {
        DataRowView row = values[0] as DataRowView;
        DataGridColumn column = values[1] as DataGridColumn;
        return row != null && column != null
            ? row[column.SortMemberPath]
            : DependencyProperty.UnsetValue;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) {
        throw new NotSupportedException();
    }
}

XAML:

    <Style x:Key="{x:Type DataGridCell}" TargetType="{x:Type DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel>
                        <TextBlock x:Name="TextOK" Text="OK" Visibility="Collapsed" />
                    </StackPanel>
                    <ControlTemplate.Triggers>
                        <DataTrigger Value="True">
                            <DataTrigger.Binding>
                                <MultiBinding Converter="{StaticResource RowColumnToCellConverter}">
                                    <Binding />
                                    <Binding RelativeSource="{x:Static RelativeSource.Self}" Path="Column" />
                                </MultiBinding>
                            </DataTrigger.Binding>
                            <Setter TargetName="TextOK" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我用了一个的TextBlock 而不是图像进行测试,但code将是相同的。只要避免定义样式的形象,如果能直接在 DataGridCell 的风格来完成。

I used a TextBlock instead an image for testing, but the code will be the same. Just avoid defining a style for the image if that can be done directly in the DataGridCell's style.

这篇关于动态绑定在WPF DataGridCell模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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