- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以将 ValueConverter 与内联一起使用?我需要将 ListBox 中每一行的某些部分加粗。
<TextBlock>
<TextBlock.Inlines>
<MultiBinding Converter="{StaticResource InlinesConverter}">
<Binding RelativeSource="{RelativeSource Self}" Path="FName"/>
</MultiBinding>
</TextBlock.Inlines>
</TextBlock>
它编译但我得到:不能在“InlineCollection”集合中使用“MultiBinding”。只能在 DependencyObject 的 DependencyProperty 上设置“MultiBinding”。
如果这不可能,您建议采用什么方法将整个 TextBlock 传递给 IValueConverter?
最佳答案
正如其他人指出的那样,Inlines
不是 Dependency Property
,这就是您收到该错误的原因。当我过去遇到类似情况时,我发现 Attached Properties
/Behaviors
是一个很好的解决方案。我将假设 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 命名空间):
<TextBlock my:InlinesBehaviors.BoldInlines="{Binding FName}" />
上面的绑定(bind)可以是多重绑定(bind),你可以使用转换器,或者其他什么。 附加行为
非常强大,这似乎是一个使用它们的好地方。
另外,如果您有兴趣,这里是 article关于附加行为
。
关于wpf - 将 ValueConverter 与内联一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16737811/
有没有一种方法可以在不在资源中定义的情况下使用 ValueConverter?因为语法非常冗长。 最佳答案 您可以使用 MarkupExtension 来最小化所需的 xaml 代码量。例如: pub
使用 Silverlight 4,我试图在我的 UserControl 的资源部分中初始化一个转换器,并引用我的控件中的一个对象。当我尝试运行应用程序时出现此异常,注意实际 XAML 文件中的第 16
我有一个绑定(bind)到集合的组合框。集合中的每个项目都有一个可能包含也可能不包含项目的子集合。我有第二个组合框,子集合绑定(bind)到该组合框。如果子集合有项目,我只想显示 (=Visibili
在与 Binding 一起使用的 Converter 中,有些东西我无法理解。 :-( 我用 Mvvm-Light 创建了一个简单的例子...... 这里是完整的解决方案:https://testbi
应该实现两个函数,我想知道我需要对输入进行哪种类型的验证以及如何管理错误。 Public Function Convert(ByVal value As Object, ByVal targetTyp
是否可以将 ValueConverter 与内联一起使用?我需要将 ListBox 中每一行的某些部分加粗。 它编译但我得到:不能在“Inl
我正在使用 MVVM,以防万一。 我的 MainWindowViewModel 有两个 DependencyProperties,TheList 和 TheSelectedItem。 TheList
我正在尝试根据单元格的值所属的 ObservableCollection 中的最小值或最大值来更改数据网格单元格的颜色。 现在我有以下数据网格和样式模板:
为什么我的 ValueConverter 没有被触发? 当我的 View 模型的构造函数被执行时,我的值转换器被触发。但是,当我为 Cells 属性分配新值时,它不会被触发。 我希望这一行能够触发我的
关于Nerd Plus Art今天的博客,有一篇关于为箭头创建WPF资源的帖子,作者经常使用。我有一个带有后退和前进按钮的辅助项目,所以我认为向左和向右箭头在这些按钮上效果很好。 我将 LeftArr
我想采用以下 ValueConverter 并使其可重用于许多 View 。 export class ProductFilterValueConverter { toView(array,
我有一个 ValueConverter 用于在 StoryBoard 动画中绑定(bind)“To”值,类似于答案 - WPF animation: binding to the “To” attri
我正在尝试将 @string/something 传递给一个值转换器,以便我可以使用它来格式化输出,假设我有一个 DateTime 值,我想将类似 "Signup Date: {0}" 的内容传递给
我一直在 WPF 中使用 Singleton 值转换器。但最近我和一位同事发生了争执,他说使用 valueconverters 的单例实例是不好的,因为它们只会被应用程序域卸载处理。他建议单例转换器可
我有值转换器,它可以在控件的文本是否以 http 开头时转换控件的可见性方面。下面是代码。 public class HTTPVisibilityConverter : IValueConverter
在我的 Grails 2.3.7 应用程序中,我有以下命令对象 class UpdateThresholdsCommand { Double threshold_1 Double th
假设您有一个用户选择列表。根据用户所做的选择,其他选择可能不再可用。 我有一个 ValueConverter 可以处理这个问题。如果它知道进行了哪些选择,它将返回一个值,指示正在转换的项目是否仍然可选
我有一个 Window 类,其中我有几个 TextBlock 元素,它们应该通过 的值接收 Background 颜色绑定(bind) 属性。第一个“Converter binding”工作正常并且可
有没有办法让 WPF 自动将 Converter 应用于特定类型的所有绑定(bind)? 我看过 this question , 但它涵盖了不同的情况(本地化),因此没有令人满意的答案。 我的问题:我
我正在尝试编写一个值转换器,用于将 WPF ToggleButton 的 Boolean IsChecked 属性绑定(bind)到我的模型中的非 bool 值(恰好是 double 值)。我编写的转
我是一名优秀的程序员,十分优秀!