2

我确实有一个 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>
4

2 回答 2

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 属性,这是一个简单的示例:

自定义控件

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); }

        }
    }
}

主窗口.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>
于 2012-11-18T20:30:44.527 回答
0

您可以将您的 ColorBrush 添加到 ResourceDictionary,通过 x:Key 属性给它一个键,并通过模板和“真实”按钮的 StaticResource 或 DynamicResource 标记扩展来引用该资源(我假设您的意思是按钮,可能在 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>

于 2012-11-18T19:18:35.960 回答