.net 在 WPF 数据网格文本列中绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 11:08:38  来源:igfitidea点击:

Binding in a WPF data grid text column

.netwpfdata-bindingxamldatagrid

提问by Matthew Maravillas

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}"/>

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

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

<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')

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.

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

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.

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

Anyone know what I'm doing wrong here?

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

Edit:

编辑:

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

感谢 Jared 的回复,我发现了以下内容:

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

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

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.

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

Code behind:

后面的代码:

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

XAML:

XAML:

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

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?

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

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

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

采纳答案by Matthew Maravillas

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

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

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

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

Following this example, I changed my DataGridTextColumn definition to:

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

<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>

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

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

回答by Bryan Anderson

Try

尝试

TextBlock.FontFamily="{Binding Font.Name}"

Sometimes the binding system has a problem finding where a property is declared so you need to give it some help.

有时绑定系统在查找属性声明的位置时会出现问题,因此您需要提供一些帮助。