简体   繁体   English

绑定到Datagrid的ItemsSource集合属性,而不是单个项目

[英]Binding to Datagrid's ItemsSource collection properties instead of single items

I'm confused while trying to bind some properties housed inside a collection rather than the properties of the elements. 在尝试绑定集合中的某些属性而不是元素的属性时,我感到困惑。 I'm not even sure how to phrase it right... code might explain better: here are the types (not actual code, I've shortened it to the basics): 我什至不知道该如何正确表达……代码可能会更好地解释:这是类型(不是实际的代码,我将其简化为基本内容):

public class myType
{
    public int P {get;set;}
}
public class myTypeCollection : ObservableCollection<myType>
{
    public int Ptotal {get { return this.Items.Select(i=>i.P).Aggregate((c,t)=>t = t + c); }}
    public int Pmin { get { this.Items.Min(i => i.P); } } //concept
    public int Pmax { get { this.Items.Max(i => i.P); } } //concept
}

They're being used in a templated control, whose XAML looks like this: (adding comments to make it as clear as i'm able to) 它们被用于XAML看起来像这样的模板化控件中:(添加注释以使其尽可能清晰)

<!-- myGridObject = new myTemplatedControl(); -->
<!-- myGridObject.DataContext = new myTypeCollection(); -->
<!-- NOTE: collection obviously is NOT empty in the real code -->
<sdk:DataGrid ItemsSource={Binding DataContext}> 
    <sdk:DataGridTemplateColumn Width="Auto">
        <sdk:DataGridTemplateColumn.HeaderStyle>
            <Style TargetType="sdk:DataGridColumnHeader">
                <Setter Property="ContentTemplate">
                    <Setter.Value>
                        <DataTemplate>

                            <!-- ?????? write out Ptotal in the header of the column ??????? -->
                            <!-- This throws a binding-related ArgumentException -->
                            <TextBlock Text="{Binding ???? Ptotal ?????}" />

    <!-- closing tags cut off -->
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding P}" />
    <!-- closing tags cut off once more-->

{Binding P} works as expected, since P is a property of the items, but how do I access the collection's properties like Ptotal , Pmin , etc. ? {Binding P}可以按预期工作,因为P是项的属性,但是我如何访问集合的属性,如PtotalPmin等?

Thanks for taking the time to read this. 感谢您抽时间阅读。 If any info is missing just point it out and I'll post it. 如果缺少任何信息,请指出来,我将其发布。

So you need the collection object as your binding source. 因此,您需要将集合对象作为绑定源。

You need these: 您需要这些:

RelativeSource MarkupExtension 相对源标记扩展

Binding.RelativeSource Property Binding.RelativeSource属性

Something like this (not tested): 像这样的东西(未经测试):

    <TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type myTemplatedControl}}, Path=DataContext.Ptotal}" />

If the DataGrid is inside your custom myTemplatedControl . 如果DataGrid在您的自定义myTemplatedControl内部。 It not totally clear for me what is myGridObject exactly. 对我来说,还不完全清楚myGridObject到底是什么。 The main idea is: 主要思想是:

As the MSDN documentation says: Binding.RelativeSource Gets or sets the binding source by specifying its location relative to the position of the binding target. 正如MSDN文档所说:Binding.RelativeSource Gets or sets the binding source by specifying its location relative to the position of the binding target.

If you stuck with the x:Type extension, here is a link about it, so you can use it with your custom control: 如果您坚持使用x:Type扩展名,那么这里是一个链接,因此您可以将其与自定义控件一起使用:

X:Type X:类型

Another approach is if you name your container element (where your collection is the datacontext), then you can set that element as the binding source: 另一种方法是,如果您命名容器元素(其中集合是数据上下文),则可以将该元素设置为绑定源:

<TextBlock Text="{Binding ElementName=yourElementName, Path=DataContext.Ptotal}" />

I think the issue is that the DataGrid is bound to the collection, and each row is bound to an individual item, not the collection. 我认为问题在于DataGrid绑定到集合,并且每一行都绑定到单个项,而不是集合。 You need to gain access a level up the chain (back to the collection itself). 您需要访问上一级链(返回到集合本身)。

If you're running Silverlight 4+ you can use a relativesource. 如果您运行的是Silverlight 4+,则可以使用相对资源。 For instance: 例如:

<TextBlock Text="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType=sdk:DataGrid, AncestorLevel=1}, Path=DataContext.Count}"

Otherwise perhaps create a static access to the context to access it via the binding Source 否则,可能会创建对上下文的静态访问以通过绑定Source访问它

Turns out the customer changed his mind about the grid headers, he doesn't want total displayed in the header anymore. 事实证明,客户改变了主意,不再希望网格标题中显示总计。

By the way, I must have tried 20 different approached including patching in various types of converters but I haven't been able to accomplish this not-as-simple-as-it-looks-apparently task. 顺便说一句,我一定已经尝试了20种不同的方法,包括在各种类型的转换器中进行修补,但是我却无法完成看起来并非如此简单的任务。

Thanks again for the nonetheless interesting suggestions. 再次感谢您提出的有趣建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF DataGrid 的 Items 和 ItemsSource 属性之间有什么区别? - What's the difference between a WPF DataGrid's Items and ItemsSource properties? WPF DataGrid:DataGridComboxBox ItemsSource 绑定到集合的集合 - WPF DataGrid: DataGridComboxBox ItemsSource Binding to a Collection of Collections 将DataGridComboBoxColumn绑定到C#中的DataGrid的ItemsSource - Binding a DataGridComboBoxColumn to the DataGrid's ItemsSource in C# 如何为用作DataGrid的ItemsSource的项目的collection属性中的每个项目生成和绑定列 - How to generate and bind a column for each item in the collection property of items used as DataGrid's ItemsSource 更新DataGrid的ItemsSource中的单个项目 - Update a single item in a DataGrid's ItemsSource 在ContentPersenter中绑定ItemsDataGrid的源 - Binding ItemsSource of Datagrid in a ContentPersenter Datagrid项目源和绑定问题 - Datagrid itemssource and binding issue 使用LINQ to SQL类的DataGrid的ItemsSource绑定窗口为空 - DataGrid's ItemsSource Binding window empty using LINQ to SQL Classes 如何将多个项目添加到ItemsSource WPF多个集合绑定中? - how to add multiple items into ItemsSource WPF multiple collection binding? 在DataGrid RowDetailsTemplate中绑定ComboBox ItemsSource - Binding ComboBox ItemsSource in DataGrid RowDetailsTemplate
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM