C# 如何扩展控件模板(例如,使用第三种颜色)
我有一个WPF按钮的控制模板。在模板中,我可以使用按钮的Fillcolor和Backcolor。但是,有没有可能定义第三种颜色,可以在模板中使用,以后可以在真实的按钮中使用 下面是一个圆形按钮的示例。我想为ToggleButton.IsChecked状态添加颜色C# 如何扩展控件模板(例如,使用第三种颜色),c#,wpf,controltemplate,C#,Wpf,Controltemplate,我有一个WPF按钮的控制模板。在模板中,我可以使用按钮的Fillcolor和Backcolor。但是,有没有可能定义第三种颜色,可以在模板中使用,以后可以在真实的按钮中使用 下面是一个圆形按钮的示例。我想为ToggleButton.IsChecked状态添加颜色 <ControlTemplate x:Key="MyButtonTemplate" TargetType="{x:Type ToggleButton}"> <Grid> <Ellip
<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>
您可以将ColorBrush添加到ResourceDictionary,通过x:key属性为其提供一个键,并通过模板和“real”按钮(我假设您指的是按钮的实例,可能是在xaml中定义的)的StaticResource或DynamicSource标记扩展来引用该资源。这需要通过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>
我不确定是否要将其绑定到属性或仅使用笔刷资源,以下是使用资源的示例:
<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>
main window.xaml
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); }
}
}
}
<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>
我不懂刷子的部分。你能给我一个XAML的样品吗?我编辑了上面的问题,并添加了一个ToggleButton模板,我希望该模板的IsChecked状态为第三种颜色。另外,Value=“{Binding Path=Foreground,RelativeSource='{RelativeSource TemplatedParent}}}”与说明{TemplateBinding Foreground}相同,但BorderBrush不是按钮的新属性。我的问题是,我是否可以添加新属性,如ToggleButton.MyOwnColor,并在绘图的控制模板中使用它。是的,但为什么要这样做。如果你下定决心这样做,你可以查看附加属性。wpf博士有一些很好的代码片段来生成锅炉板代码。对我来说,这将是一个复杂的问题。我已经编辑了你的标题。请看“”,其中的共识是“不,他们不应该”。好的,谢谢。很高兴知道在您的示例中,您无法访问或更改ToggleButton实例本身中的颜色/笔刷。因此,控件模板中的ControlPressedColor是静态的。我的问题是,如果我能定义一些东西,比如模板中使用了MyColor。是的,谢谢。我认为不实现派生的button类是可能的。但这会有用的,谢谢。