Wpf ContentPresenter TextBlock.FontFamily绑定不工作

Wpf ContentPresenter TextBlock.FontFamily绑定不工作,wpf,xaml,contentpresenter,Wpf,Xaml,Contentpresenter,我有一个ContentPresenter,希望通过将每个项目绑定到FontFamily来使用不同的字体呈现每个项目,但它不起作用。奇怪的是,绑定适用于TextBlock,但不适用于ContentPresenter?这是我的xaml <ContentPresenter Width="100" Content="{Binding Name}" TextBlock.FontFamily="{Binding TextFont}" TextBlock.Foregro

我有一个ContentPresenter,希望通过将每个项目绑定到FontFamily来使用不同的字体呈现每个项目,但它不起作用。奇怪的是,绑定适用于TextBlock,但不适用于ContentPresenter?这是我的xaml

<ContentPresenter 
    Width="100"
    Content="{Binding Name}"
    TextBlock.FontFamily="{Binding TextFont}"
    TextBlock.Foreground="{Binding BindedTextColor}"/>
<TextBlock
    Text="{Binding FontName}"
    FontFamily="{Binding TextFont}"
    Foreground="{Binding BindedTextColor}"/>
这使得:

TextBlock和ContentPresent都将其FontFamily绑定到TextFont。为什么“住宅”一词不能用阿尔及利亚字体家族来表达

如果我将TextBlock.FontFamily={Binding TextFont}更改为像这样的硬接线TextBlock.FontFamily=Algeria,我会得到这个


如何使用绑定?

如果在运行时查看VS中的“输出”窗格,您会发现ContentPresenter上的绑定正在查看ContentPresenter的DataContext属性TextFont和BindedTextColor,正如预期的那样-但DataContext不是您所期望的。这是ContentPresenter的内容属性

是一个非常专业的控件。你不想要一个在这里。仅在ControlTemplate中按指示使用。对于此应用程序,请使用ContentControl或标签

如果您在没有绑定的情况下设置这些属性,那么DataContext就不是问题,您可以得到所期望的结果

System.Windows.Data错误:40:BindingExpression路径错误:在“object”字符串“HashCode=966822854”上找不到“TextFont”属性。BindingExpression:Path=TextFont;DataItem='String'HashCode=966822854;目标元素为“ContentPresenter”名称=;目标属性为“FontFamily”类型“FontFamily”

System.Windows.Data错误:40:BindingExpression路径错误:在“object”字符串“HashCode=966822854”上找不到“BindedTextColor”属性。BindingExpression:Path=BindedTextColor;DataItem='String'HashCode=966822854;目标元素为“ContentPresenter”名称=;目标属性为“前景”类型“笔刷”

每当绑定出现明显错误时,请在输出窗格中查找错误。如果您没有立即解决问题,请添加一个跟踪,您将在“输出”窗格中获得成堆的调试信息:

<ContentPresenter 
    Width="100"
    Content="{Binding ID}"
    TextBlock.FontFamily="{Binding TextFont, PresentationTraceSources.TraceLevel=High}"
    TextBlock.Foreground="{Binding BindedTextColor}"
    />