是否有可能对参数在资源DataGrid中CellTemplate的约束力? [英] Is it possible to parameterize the binding on a DataGrid CellTemplate in a resource?

查看:190
本文介绍了是否有可能对参数在资源DataGrid中CellTemplate的约束力?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我放在一起一些通用CellTemplate样式WPF的网格(WPFToolKit DataGrid中),我不知道语法的使用,使结合通用。因此,例如,我有这个模板,它变成了红色值如果该值是负的:

I'm putting together some generic CellTemplate styles for a WPF grid (WPFToolKit DataGrid), and I’m not sure of the syntax to use to make the binding generic. So for example, I have this template, which turns the value red if the value is negative:

<DataTemplate x:Key="RedNegativeCellTemplate">
    <TextBlock Name="QuantityTextBox" Text="{Binding Quantity, StringFormat='c', Mode=OneWay}"/>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Quantity, Converter={StaticResource SignConverter}}" Value="-1">
            <Setter TargetName="QuantityTextBox" Property="Foreground" Value="Red"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

您会发现,这对文本框列值的绑定'的数量的' - 即场/列的绑定来自是明确的。

You’ll notice that this has a binding on the Text box to the column value ‘Quantity’ - i.e., the field/column that the binding comes from is explicit.

所以我可以在我的WPF DataGrid中按如下方式使用这样的:

So I can use this in my WPF DataGrid as follows:

<sdk:DataGrid ItemsSource="{Binding MyDataSource}" AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>

        <sdk:DataGridTemplateColumn Header="Net Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/>

    </sdk:DataGrid.Columns>
</sdk:DataGrid>

但是......我真的很想做的是有场模板结合通用的,这样我就可以重新使用模板如下:

But… what I’d really like to do is to have the field binding generic for the template, so that I can re-use the template as follows:

<sdk:DataGrid ItemsSource="{Binding OrdersQuery}"AutoGenerateColumns="False">
    <sdk:DataGrid.Columns>
        <sdk:DataGridTemplateColumn Header="Quantity" CellTemplate="{StaticResource RedNegativeCellTemplate}"/>
        <sdk:DataGridTemplateColumn Header="Price" CellTemplate="{StaticResource RedNegativeCellTemplate}"/>
        <sdk:DataGridTemplateColumn Header="Total" CellTemplate="{StaticResource RedNegativeCellTemplate}"/>
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

现在,这里的问题是,似乎没有被参数化在CellTemplate结合的方式。但我不愿意要有3+线的ControlTemplate对于每个列使用该模板,因为它杂波的XAML,使得它远不如可读性(更不用提,如果我决定的事实更改单元格模板,把周围的边框文本框,我不得不修改它在多个地方。

Now, the problem here is that there doesn't seem to be a way to parameterize the binding in the CellTemplate. But I'm loath to have to have 3+ lines of controltemplate for every column that uses this template, as it clutters the XAML and makes it far less readable (not to mention the fact that if I decide to change the cell template to put a border around the textbox, I'd have to modify it in multiple places.

所以我以为CellTemplate内的结合应该是这个样子(请注意,我们使用的绑定路径'。')

So I'm thinking that the binding within the CellTemplate should look something like this (note that we're using '.' for the binding path):

            Text="{Binding Path=., StringFormat='c', Mode=OneWay}"

然后莫名其妙定置从DataGridTemplateColumn声明DataContext的 - 但我看不出如何做到这一点。

And then somehow set the datacontext from the DataGridTemplateColumn declaration – but I can’t see how to do it.

任何想法如何做到这一点?

Any idea how to do this?

推荐答案

有关这个,你可以使用的 DataTemplateSelector 这实际上是pretty容易实现。
结果如果你需要一些进一步的解释和例子 - 让我知道

For this you can use DataTemplateSelector which is actually pretty straightforward to implement.
If you need some further explanations and examples - let me know.

这篇关于是否有可能对参数在资源DataGrid中CellTemplate的约束力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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