我是WPF的新手,我正在努力寻找解决方案,我正在努力做的事情,因为我仍然对我是否正确地做这件事有点不确定。WPF样式触发器TemplateBinding
我有一个按钮
<Style x:Key="ToolBarButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Margin" Value="5" />
<Setter Property="BorderBrush" Value="White" />
<Setter Property="Background" Value="{DynamicResource CompanyBlue}" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontSize" Value="20" />
<Setter Property="Width" Value="100" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="ButtonBorder" BorderThickness="5" CornerRadius="5"
BorderBrush="{TemplateBinding BorderBrush}"
Background="{TemplateBinding Background}"
Width="{TemplateBinding Width}">
<ContentPresenter Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Button.Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0,-0.2" EndPoint="0,1.2">
<LinearGradientBrush.GradientStops>
<GradientStop Color="White" Offset="0" />
<GradientStop Color="{ORIGINAL-COLOR}" Offset="0.5" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush.GradientStops>
</LinearGradientBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
在XAML,在那里我已经把占位{ORIGINAL-COLOR}
定义了如下的风格,我基本上是想这是与{TemplateBinding Background}
二手先前设置控件的值风格的模板。
我见过建议我应该使用{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}
但这不起作用。
在此先感谢您的帮助。
以何种方式是不工作?什么是信息/错误或者它是否默默地失败。确保你检查了输出窗口,因为这是WPF转储大量调试信息的地方。 – pbalaga 2013-03-03 15:21:13
由于'Background'的返回类型是'Brush'而不是'Color',因此不能将'Color'与'Background'属性绑定。 – 2013-03-03 15:22:17
我得到的错误如下:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:路径= Background.Color;的DataItem = NULL;目标元素是'GradientStop'(HashCode = 19346574);目标属性是'颜色'(类型'颜色') – 2013-03-03 15:29:06