我有一个WPF按钮的控件模板。在模板中,我可以使用按钮的Fillcolor和Backcolor。但是,是否可以定义可以在模板中使用的第三种颜色,以及稍后在真实按钮中使用的颜色?
这是一个圆形按钮的例子。我想为ToggleButton.IsChecked状态添加一种颜色。
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}">
<Grid>
<Ellipse x:Name="outerCircle" Width="Auto" Height="Auto" StrokeThickness="4" Fill="White" Stroke="Gray"/>
<Label x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Gray"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="outerCircle" Property="Fill" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
<Setter TargetName="content" Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
答案 0 :(得分:2)
我不确定你是否想将它绑定到属性或只是使用画笔资源,这是一个使用资源的例子:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="ControlPressedColor">#FF211AA9</SolidColorBrush >
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}">
<Grid>
<Ellipse x:Name="outerCircle" Width="Auto" Height="Auto" StrokeThickness="4" Fill="White" Stroke="Gray"/>
<Label x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Gray"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{StaticResource ControlPressedColor}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="outerCircle" Property="Fill" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
<Setter TargetName="content" Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<ToggleButton Template="{StaticResource MyButtonTemplate}" Height="50" Width="50"></ToggleButton>
</Grid>
</Window>
根据您希望能够在ToggleButton上设置属性的说明,您需要使用Dependecy Property这是一个简单的示例:
自定义控制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
public class CustomControl2 : System.Windows.Controls.Primitives.ToggleButton
{
public static readonly DependencyProperty myFillColorProperty = DependencyProperty.Register("myFillColor",typeof(SolidColorBrush),typeof(System.Windows.Controls.Primitives.ToggleButton));
static CustomControl2()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl2), new FrameworkPropertyMetadata(typeof(CustomControl2)));
}
public SolidColorBrush myFillColor
{
get { return (SolidColorBrush)GetValue(myFillColorProperty); }
set { SetValue(myFillColorProperty, value); }
}
}
}
<强> MainWindow.xaml 强>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my ="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}">
<Grid>
<Ellipse x:Name="outerCircle" Width="Auto" Height="Auto" StrokeThickness="4" Fill="White" Stroke="Gray"/>
<Label x:Name="content" Content="{TemplateBinding Content}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Gray"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=myFillColor, RelativeSource='{RelativeSource TemplatedParent}'}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="outerCircle" Property="Fill" Value="LightGray"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="outerCircle" Property="Fill" Value="{Binding Path=Foreground, RelativeSource='{RelativeSource TemplatedParent}'}"/>
<Setter TargetName="content" Property="Foreground" Value="Gray"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<my:CustomControl2 myFillColor="Red" Template="{StaticResource MyButtonTemplate}" Height="50" Width="50"></my:CustomControl2>
</Grid>
</Window>
答案 1 :(得分:0)
你可以将你的ColorBrush添加到ResourceDictionary,通过x:Key属性给它一个键,并通过模板和“真实”按钮(我认为你的意思)通过StaticResource或DynamicResource标记扩展引用该资源Button的一个实例,可能在xaml中定义。这要求您通过ResourceDictionary.MergedDictionaries属性合并包含该键的字典。
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="MySharedColor" Color="Red"/>
<ControlTemplate x:Key="MyTemplate" TargetType="{x:Type Button}">
<Border Background="{StaticResource MySharedColor}"/>
</ControlTemplate>
</Window.Resources>
<Grid>
<Button Style="{StaticResource MyTemplate}" BorderBrush="{StaticResource MySharedColor}"/>
</Grid>