WPF - 如何绑定 DataGridTemplateColumn

发布于 2024-08-28 05:55:34 字数 1045 浏览 12 评论 0原文

我试图获取与特定 DataGridColumn 关联的属性名称,以便我可以基于该名称执行一些操作。当用户单击列标题上的上下文菜单项时,将调用此函数...

这对于开箱即用的现成滚动列类型(如 DataGridTextColumn )来说很好,因为它们是绑定的,但问题是我的一些列是DataGridTemplateColumns,它们没有绑定。

private void GroupByField_Click (object sender, RoutedEventArgs e){
        MenuItem mi = (MenuItem)sender;
        ContextMenu cm = (ContextMenu) mi.Parent;
        DataGridColumnHeader dgch = (DataGridColumnHeader) cm.PlacementTarget;  
        DataGridBoundColumn dgbc = (DataGridBoundColumn) dgch.Column;
        Binding binding = (Binding) dgbc.Binding;
        string BoundPropName = binding.Path.Path;

        //Do stuff based on bound property name here...
    }

因此,以我的 Name 列为例...它是一个 DataGridTemplateColumn (因为其中有图像和其他一些内容)。因此,它实际上并未绑定到“Name”属性......但我希望如此,以便上面的代码能够工作。

我的问题实际上分为两部分:

  1. 是否可以使 DataGridTemplateColumn 绑定,以便上面的代码可以工作?我可以以某种方式将其绑定到属性吗?

  2. 或者我是否需要完全不同的东西,并更改上面的代码?

提前致谢!

I am trying to get the name of the property associated with a particular DataGridColumn, so that I can then do some stuff based on that. This function is called when the user clicks context menu item on the column's header...

This is fine for the out-of-the-box ready-rolled column types like DataGridTextColumn, since they are bound, but the problem is that some of my columns are DataGridTemplateColumns, which are not bound.

private void GroupByField_Click (object sender, RoutedEventArgs e){
        MenuItem mi = (MenuItem)sender;
        ContextMenu cm = (ContextMenu) mi.Parent;
        DataGridColumnHeader dgch = (DataGridColumnHeader) cm.PlacementTarget;  
        DataGridBoundColumn dgbc = (DataGridBoundColumn) dgch.Column;
        Binding binding = (Binding) dgbc.Binding;
        string BoundPropName = binding.Path.Path;

        //Do stuff based on bound property name here...
    }

So, take for example my Name column... it's a DataGridTemplateColumn (since it has an image and some other stuff in there). Therefore, it is not actually bound to the 'Name' property... but I would like to be, so that the above code will work.

My question is two-part, really:

  1. Is it possible to make a DataGridTemplateColumn be BOUND, so that the above code would work? Can I bind it somehow to a property?

  2. Or do I need to something entirely different, and change the code above?

Thanks in advance!

AT

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱的那么颓废 2024-09-04 05:55:34

尽管您无法绑定模板列,但您可以绑定该列中包含的控件之一。这就是我解决类似问题的方法:

<DataGridTemplateColumn Header="ColumnHeader">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                 <local:CustomisedUIElement Text="{Binding Path=PropertyToBindTo}"/>
           </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

如果我正确理解了最初的示例,这将意味着更改 GroupByField_Click() 方法的逻辑以检查发送列是否是模板列,然后查看它包含的元素以获得 Binding 对象。

Although you can't bind a template column, you can bind one of the controls held in that column. This is how I solved a similar problem:

<DataGridTemplateColumn Header="ColumnHeader">
     <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
                 <local:CustomisedUIElement Text="{Binding Path=PropertyToBindTo}"/>
           </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

If I've understood the initial example properly, this would mean changing the logic of the GroupByField_Click() method to check whether the sending column was a template column and then looking at the elements it contained to obtain the Binding object.

画中仙 2024-09-04 05:55:34

对我来说,DataGridTemplateColumnClipboardContentBinding 是一个解决方案:

Private Function FindBoundProperty(ByVal col As DataGridColumn) As String

    Dim boundColumn As DataGridBoundColumn = TryCast(col, DataGridBoundColumn)
    Dim boundPropertyName As String = ""
    Dim binding As Binding
    If col.DependencyObjectType.Name = "DataGridTextColumn" Then
        binding = TryCast(boundColumn.Binding, Binding)
        boundPropertyName = binding.Path.Path
    End If
    If col.DependencyObjectType.Name = "DataGridTemplateColumn" Then
        binding = TryCast(col.ClipboardContentBinding, Binding)
        boundPropertyName = binding.Path.Path
    End If
    Return boundPropertyName

End Function

For me, ClipboardContentBinding of DataGridTemplateColumn is a solution:

Private Function FindBoundProperty(ByVal col As DataGridColumn) As String

    Dim boundColumn As DataGridBoundColumn = TryCast(col, DataGridBoundColumn)
    Dim boundPropertyName As String = ""
    Dim binding As Binding
    If col.DependencyObjectType.Name = "DataGridTextColumn" Then
        binding = TryCast(boundColumn.Binding, Binding)
        boundPropertyName = binding.Path.Path
    End If
    If col.DependencyObjectType.Name = "DataGridTemplateColumn" Then
        binding = TryCast(col.ClipboardContentBinding, Binding)
        boundPropertyName = binding.Path.Path
    End If
    Return boundPropertyName

End Function
才能让你更想念 2024-09-04 05:55:34

这是一个棘手的问题。我们通过遍历其祖父 UserControl(我们在 UserControl 内有 DataGrid)来实现绑定,并且 UserControl 绑定到 Presenter(在我们的例子中为 Model)。
在下面的代码中,检查放置在 DataGridTemplateColumn 内的 AutoCompleteBox 的 SelectedItem 属性。

<wpfToolkit:DataGridTemplateColumn  Header="{x:Static resources:Store.ItemNameC}" Width="0.60*">
  <wpfToolkit:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <extended:HOAutoCompleteBox
                            IsTextCompletionEnabled ="True"
                            x:Name="ItemAutoCompleteBox"
                            Populating="ItemAutoCompleteBox_Populating"
                            DefaultType="HealthObject.ObjectModel.pSearchStockItemResult,HealthObject.ObjectModel"
                            Text="{Binding Path= ItemName, Mode=TwoWay}" 
                            <!--- **** HERE IS THE BINDING SAMPLE *****-->
            SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},  Path=Model.SelectedStockItem, Mode=TwoWay}">
        </extended:HOAutoCompleteBox>
    </DataTemplate>
  </wpfToolkit:DataGridTemplateColumn.CellEditingTemplate>

</wpfToolkit:DataGridTemplateColumn>

It's a tricky one. We achieved the binding by traversing to its grandparent UserControl (we had DataGrid inside a UserControl) and the UserControl was bound to a Presenter (Model in our case).
In the code below, check the property SelectedItem of AutoCompleteBox placed inside the DataGridTemplateColumn.

<wpfToolkit:DataGridTemplateColumn  Header="{x:Static resources:Store.ItemNameC}" Width="0.60*">
  <wpfToolkit:DataGridTemplateColumn.CellEditingTemplate>
    <DataTemplate>
        <extended:HOAutoCompleteBox
                            IsTextCompletionEnabled ="True"
                            x:Name="ItemAutoCompleteBox"
                            Populating="ItemAutoCompleteBox_Populating"
                            DefaultType="HealthObject.ObjectModel.pSearchStockItemResult,HealthObject.ObjectModel"
                            Text="{Binding Path= ItemName, Mode=TwoWay}" 
                            <!--- **** HERE IS THE BINDING SAMPLE *****-->
            SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},  Path=Model.SelectedStockItem, Mode=TwoWay}">
        </extended:HOAutoCompleteBox>
    </DataTemplate>
  </wpfToolkit:DataGridTemplateColumn.CellEditingTemplate>

</wpfToolkit:DataGridTemplateColumn>
流心雨 2024-09-04 05:55:34

您可以使用
dgbc.ClipboardContentBinding;

You may use
dgbc.ClipboardContentBinding;

汹涌人海 2024-09-04 05:55:34
  1. 否,因为 DataGridTemplateColumn 不是从 DataGridBoundColumn 继承,因此转换为 DataGridBoundColumn 将会失败。
    要使上述代码正常工作,所有列都必须继承自 DataGridBoundColumn 抽象类。因此,制作自定义派生 Column 类而不是 DataGridTemplateColumn 应该可行。

  2. 您可以简单地填充

    字典BoundPropName;

    初始化然后执行

    var propName = BoundPropName[dgch.Column]

  1. No, because DataGridTemplateColumn doesn't inherit from DataGridBoundColumn, so the cast to DataGridBoundColumn would fail.
    To make above code work all of your columns must inherit from DataGridBoundColumn abstract class. So making custom derived Column classes instead of DataGridTemplateColumn should work.

  2. You could simply populate a

    Dictionary<DataGridColumn, string> BoundPropName;

    on initialization and then do

    var propName = BoundPropName[dgch.Column]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文