在 WPF 数据网格文本列中绑定 [英] Binding in a WPF data grid text column

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

问题描述

我正在尝试构建一个数据网格,其中一列是以该字体显示的字体名称.以前,我使用一个列表框,在其中定义了以下模板:

I'm trying to build a data grid where one of the columns is a font name displayed in that font. Previously, I was working with a list box where I had defined the following template:

<TextBlock Text="{Binding Path=Name}" FontFamily="{Binding Path=Name}"/>

这工作得很好.所以,我调整了数据结构(Name 变成了 Font.Name)并移到数据网格上尝试:

This worked just fine. So, I tweaked the data structure (Name became Font.Name) and moved onto a data grid to try this:

<dg:DataGridTextColumn Binding="{Binding Font.Name}" 
    FontFamily="{Binding Font.Name}" IsReadOnly="True" Header="Font"/>

现在字体名称都以默认字体显示,出现这个错误:

Now the font names are all displayed in the default font, and I get this error:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or 
FrameworkContentElement for target element. 
BindingExpression:Path=Font.Name; DataItem=null; target element is 
'DataGridTextColumn' (HashCode=56915998); target property is 'FontFamily' 
(type 'FontFamily')

一些处理自定义控件的 Google 结果建议将属性从 DependencyObject 更改为 FrameworkElement,但我必须继承 DataGridTextColumn 并定义我自己的属性才能这样做 - 必须有更好的方法.

A few Google results dealing with custom controls suggest changing the property from DependencyObject to FrameworkElement, but I'd have to inherit DataGridTextColumn and define my own property to do so - there must be a better way.

我尝试了几种不同的绑定方法,包括尝试使用我的数据类中的不同属性(即,FontSize="{Binding FontSize}")仅更改字体大小.它们都导致了与上述相同的错误.

I've tried several different approaches to the binding, including attempting to change just the font size with a distinct property in my data class (i.e., FontSize="{Binding FontSize}"). They've all resulted in the same error as above.

有人知道我在这里做错了什么吗?

Anyone know what I'm doing wrong here?

感谢 Jared 的回复,我找到了以下内容:

Thanks to Jared's reply, I found the following:

https://docs.microsoft.com/en-us/archive/blogs/jaimer/forwarding-the-datagrids-datacontext-to-its-columns

该方法看起来不错,但我需要进行绑定以引用 DataContext 中每一行的正确元素,而不是为整列共享一个值.

The method looks sound, but I need to make a binding that references the correct element in the DataContext for each row, as opposed to sharing a single value for the entire column.

背后的代码:

fontDataGrid.DataContext = from font 
    in new InstalledFontCollection().Families;

XAML:

Binding="{Binding Font.Name}"
FontFamily="{Binding (FrameworkElement.DataContext).Font.Name, 
    RelativeSource={x:Static RelativeSource.Self}}"

使用上述 XAML 显然是不正确的,因为 DataContext 是整个字体集合.但是我无法索引集合,因为我不知道行号是什么(或者我不知道?).我可以使用某种方法来实现这一目标吗?

Using the above XAML clearly isn't correct, because DataContext is the entire collection of fonts. But I can't index the collection, since I don't know what the row number is (or do I?). Is there some approach I can use to achieve this?

还有一个次要问题 - 为什么即使没有 DataContext Binding 属性似乎也能正常工作?是在查看 ItemsSource 吗?

And a secondary question - why does the Binding attribute seem to work just fine, even without the DataContext? Is it looking at ItemsSource instead?

推荐答案

Jared 的回答是正确的,但我找到了解决我问题的具体解决方案.

Jared's answer is correct, but I've found a concrete solution that's solved my problem.

http://blogs.msdn.com/vinsibal/archive/2008/12/17/wpf-datagrid-dynamically-updating-datagridcomboboxcolumn.aspx

按照这个示例,我将 DataGridTextColumn 定义更改为:

Following this example, I changed my DataGridTextColumn definition to:

<dg:DataGridTextColumn Binding="{Binding Font.Name}" IsReadOnly="True" Header="Font">
    <dg:DataGridTextColumn.ElementStyle>
        <Style TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{Binding Font.Name}" />
        </Style>
    </dg:DataGridTextColumn.ElementStyle>
</dg:DataGridTextColumn>

而且我不需要担心继承DataContext的列.这给了我想要的结果.

And I don't need to worry about the column inheriting the DataContext. This gives me the result I want.

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

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