Wpf 使用带内联线的ValueConverter

Wpf 使用带内联线的ValueConverter,wpf,Wpf,是否可以使用带内联线的ValueConverter?我需要列表框中每行的某些部分加粗 <TextBlock> <TextBlock.Inlines> <MultiBinding Converter="{StaticResource InlinesConverter}"> <Binding RelativeSource="{RelativeSource Self}" Path="FName"/> </Mu

是否可以使用带内联线的ValueConverter?我需要列表框中每行的某些部分加粗

<TextBlock>
  <TextBlock.Inlines>
     <MultiBinding Converter="{StaticResource InlinesConverter}">
        <Binding RelativeSource="{RelativeSource Self}" Path="FName"/>
     </MultiBinding>
  </TextBlock.Inlines>  
</TextBlock>

它可以编译,但我得到: “多重绑定”不能在“InlineCollection”集合中使用。“多重绑定”只能在DependencyObject的DependencyProperty上设置


如果不可能,您建议采用什么方法将整个文本块传递给IValueConverter?

不,
内联线
不是一个
依赖属性
-这基本上就是错误消息所说的


.

正如其他人指出的,
内联线
不是一个
依赖属性
,这就是为什么会出现该错误。当我在过去遇到类似情况时,我发现
附加属性
/
行为
是一个很好的解决方案。我将假定
FName
的类型为
string
,因此附加属性也是如此。下面是一个例子:

class InlinesBehaviors
{
    public static readonly DependencyProperty BoldInlinesProperty = 
        DependencyProperty.RegisterAttached(
            "BoldInlines", typeof(string), typeof(InlinesBehaviors), 
            new PropertyMetadata(default(string), OnBoldInlinesChanged));

    private static void OnBoldInlinesChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBlock = d as TextBlock;
        if (textBlock != null) 
        {
            string fName = (string) e.NewValue; // value of FName
            // You can modify Inlines in here
            ..........
        }
    }

    public static void SetBoldInlines(TextBlock element, string value)
    {
        element.SetValue(BoldInlinesProperty, value);
    }

    public static string GetBoldInlines(TextBlock element)
    {
        return (string) element.GetValue(BoldInlinesProperty);
    }
}
然后可以按以下方式使用它(
my
是指向包含
AttachedProperty
的命名空间的xml命名空间):


上面的绑定可以是多绑定,也可以使用转换器,或者其他什么<代码>附加行为非常强大,这似乎是一个使用它们的好地方


另外,如果您感兴趣,这里有一个关于
的附加行为

我在寻找解决“不能在“InlineCollection”集合中使用“MultiBinding”相同错误的方法时遇到了这个问题。我的问题不在于
内联线
属性,而是在为我的
文本块
定义我的
多绑定
时,我无意中遗漏了
标记。只要检查一下您是否也遇到了此错误。

当只有一个值要绑定时,您不需要多重绑定。试试绑定吧,这很有道理。这也是初学者的问题,但是有没有办法用datatriggers在文本块的文本中加粗数字?我在“public static readonly dependencProperty BoldInlinesProperty=dependencProperty.RegisterAttached(“BoldInlines”)、typeof(string)、typeof(“InlinesBehaviors”)、new PropertyMetadata上有几个错误(默认值(字符串),OnBoldInlinesChanged);“我知道了,”InlinesBehaviors“应该没有引号。非常感谢!我看到许多帖子提到这是不可能的,而且没有一个快速和肮脏的解决方案被建议。我知道有人一定知道这个解决方案。我只是看到了你的评论,我很高兴你能找出我的打字错误,我可以帮助你。
<TextBlock my:InlinesBehaviors.BoldInlines="{Binding FName}" />