xaml - 是否可以在 XAML 中的 TemplateBinding 上使用转换器?

标签 xaml binding styles converter templatebinding

我正在构建一个按钮样式,该样式依赖于将颜色画笔转换为较暗的阴影来创建阴影。在常规 XAML 中,我有一个转换器,而不是我在完美工作的绑定(bind)上使用的转换器:

BorderBrush="{Binding Background, Converter={StaticResource ColourBrushToDarker}}"

但我无法让转换器在样式定义中使用 TemplateBinding。有办法吗? Visual Studio 只是不允许在 TemplateBinding 上使用转换器。

我试过以下没有运气:
Background="{Binding Converter={StaticResource ColourBrushToDarker}, ConverterParameter={Binding Path=Background}}"/>

(并且我已经尝试过用 TemplateBinding 替换 Binding 以及其他几个迭代的上述行)

这可以做到吗?我想到的另一件事是在 C# 中编写一个进行转换的属性,但样式没有代码隐藏文件。

我想要的结果是能够创建一个比 Background 属性更暗的阴影的新画笔,因此按钮始终具有比其主要背景颜色更暗的阴影。

最佳答案

经过一些(繁琐的)试验和错误后,我找到了解决方案:

Background="{Binding Background, RelativeSource={RelativeSource Mode=TemplatedParent}, Converter={StaticResource ColourBrushToDarker}}"

我仍在学习 XAML 样式,但我认为这里发生的事情是通过将相对源设置为模板父级,我可以像使用 TemplateBinding 一样使用 Binding。由于我使用的是 Binding(不是 TemplateBinding),因此我可以添加一个转换器并获得所需的结果。

关于xaml - 是否可以在 XAML 中的 TemplateBinding 上使用转换器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28801546/

相关文章:

c# - WPF - 当值为 null 或空时禁用工具提示

c# - 如何更新 MainWindow 中 StatusBarItem 中的 WPF UserControl 属性形成另一个具有不同 DataContexts 的 UserControl?

html - 在 Logo bootstrap 3 旁边输入文本

css - 有没有办法在 Angular2 组件内的 ng-content 中使用 css?

xaml - Xamarin 表单 : How to fix Activity Indicator in Centre of Screen in Scrollview

silverlight - 我可以在 Silverlight 中以声明方式将 ItemsContol.ItemsSource 绑定(bind)到 ObservableCollection 吗?

wpf - 自定义 UpdateSourceTrigger 延迟?

wpf - 将 WPF UserControl 绑定(bind)到 View 模型和隐藏代码

javafx Bindings.createStringBinding 但绑定(bind)实际上不起作用

html - 我的图像破坏了我的文本样式,我该如何解决?