参数化风格/模板在WPF?

如果我有两个200线长的控制模板,不同之处仅在几句话(几种颜色),我怎么可以使XAML可重用? 也就是说,不必复制粘贴的模板,并改变200线3个字。

下面是一个简化的例子。 两种风格之间的唯一差别是边框颜色。 所以,我可以以某种方式定义按钮样式,具有参数化色彩,并从它继承BlackButtonStyle和GrayButtonStyle,并在BlackButtonStyle和GrayButtonStyle仅指定的颜色?

<Window x:Class="WpfApplication33.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Window.Resources> <Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border BorderBrush="Black" BorderThickness="3"> <ContentControl Content="{TemplateBinding Content}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="GrayButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border BorderBrush="Gray" BorderThickness="3"> <ContentControl Content="{TemplateBinding Content}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel> <Button Content="Black Button" Style="{StaticResource BlackButtonStyle}"/> <Button Content="Gray Button" Style="{StaticResource GrayButtonStyle}"/> </StackPanel> </Window>

下面是基于该2答案的代码。 只有一种风格需要在控制设置,但遗憾的是它仍然打乱了控件的标签:

<Window x:Class="WpfApplication33.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Window.Resources> <Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Border Name="border" BorderBrush="Black" BorderThickness="3"> <ContentControl Content="{TemplateBinding Content}"/> </Border> <ControlTemplate.Triggers> <Trigger Property="Tag" Value="Gray"> <Setter TargetName="border" Property="BorderBrush" Value="Gray"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="BlackButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}"/> <Style x:Key="GrayButtonStyle" TargetType="{x:Type Button}" BasedOn="{StaticResource ButtonStyle}"> <Setter Property="Tag" Value="Gray"/> </Style> </Window.Resources> <StackPanel> <Button Content="Black Button" Style="{StaticResource BlackButtonStyle}"/> <Button Content="Gray Button" Style="{StaticResource GrayButtonStyle}"/> </StackPanel> </Window>

--------------解决方案-------------

虽然查理的关于他的例子吧,为您的特定情况下,我只是使用BorderThicknessBorderBrush属性按钮已经公开:您可以使用{TemplateBinding BorderBrush}而不是创建自己的财产。

编辑:样品XAML ......注意,我的样式默认的颜色和厚度,但这些可以内嵌覆盖...

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Page.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
<SolidColorBrush x:Key="GrayBrush" Color="Gray"/>
<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}">
<Setter Property="BorderThickness" Value="3" />
<Setter Property="BorderBrush" Value="{StaticResource BlackBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderColor="{TemplateBinding BorderThickness}">
<ContentControl Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>

<StackPanel>
<Button Content="Black Button" BorderBrush="{StaticResource BlackBrush}"
Style="{StaticResource CustomButtonStyle}"/>
<Button Content="Gray Button" BorderBrush="{StaticResource GrayBrush}"
Style="{StaticResource CustomButtonStyle}"/>
</StackPanel>

正确的方法去了解这通常是创建一个DependencyProperty上可以保持参数化数据,然后绑定到你的模板属性的类。 为了创建一个简单的例子中,我将使用Button.Tag属性,它工作得很好,用于存储一些简单的画笔:

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Page.Resources>
<SolidColorBrush x:Key="BlackBrush" Color="Black"/>
<SolidColorBrush x:Key="GrayBrush" Color="Gray"/>
<Style x:Key="CustomButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding Tag}" BorderThickness="3">
<ContentControl Content="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>

<StackPanel>
<Button Content="Black Button" Tag="{StaticResource BlackBrush}"
Style="{StaticResource CustomButtonStyle}"/>
<Button Content="Gray Button" Tag="{StaticResource GrayBrush}"
Style="{StaticResource CustomButtonStyle}"/>
</StackPanel>

看一看这个解决方案,它解决了您遇到的具体问题。

分类:WPF 时间:2015-03-15 人气:1
本文关键词: WPF
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.628 (s). 10 q(s)