【发布时间】:2016-11-26 04:41:03
【问题描述】:
目标
我有一个针对ToggleButton 的自定义控件,它也可以与Button 一起使用。所以我想为这两种类型使用一个通用的默认ControlTemplate。
我尝试的策略是在模板中设置TargetType="{x:Type ButtonBase}",如果在Button 或ToggleButton 上显式设置,则效果很好。
隐式
自定义控件库
在项目根目录下 Themes 文件夹中名为 Generic.xaml 的资源字典中...
<Style TargetType="{x:Type ButtonBase}">
<Setter Property="Template">
<Setter.Value>
<!--Modified Control Template-->
<ControlTemplate TargetType="{x:Type ButtonBase}">
在控件的类中,我使用FrameworkElement.DefaultStyleKey在其静态构造函数中设置用户控件类型的元数据...
static ContentToggle()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ContentToggle),
new FrameworkPropertyMetadata(typeof(ButtonBase)));
}
使用 WPF 应用项目
App.xaml...
<Application x:Class="Spec.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--Get a reference to the window to establish View Context-->
<RelativeSource x:Key="View" Mode="FindAncestor"
AncestorType="{x:Type Window}" />
<ResourceDictionary.MergedDictionaries>
<!--Local Style-->
<ResourceDictionary Source="pack://application:,,,/ButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml...
<Window x:Class="Spec.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="clr-namespace:ContentToggleButton;assembly=ContentToggleButton"
Title="MainWindow" Height="350" Width="525">
<Grid>
<b:ContentToggle Name="Toggle" Height="30"
Content="{Binding options, RelativeSource={StaticResource View}}"
/>
</Grid>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public List<string> options { get; set; }
public bool initialState { get; set; }
public MainWindow ()
{
options = new List<string> { "Checked", "UnChecked" };
initialState = false;
InitializeComponent();
}
}
还有一个名为 ButtonStyle.xaml 的文件定义了自定义控件要使用的画笔。它通过 App.xaml 中的合并字典在应用的根目录上公开。
结果
ContentToggle instance 的模板为 null,样式控件没有视觉效果(当我窥探该控件时,它没有子元素)。
我的理解是自动ButtonBase 样式/模板将用于我的控制。我错过了什么?
显式
如果在控件上显式声明了样式/模板,则自定义控件将按预期工作。以下适用于样式目标设置为ButtonBase...
使用 WPF 应用项目
在 App.xaml 中...
<Application x:Class="Spec.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!--Get a reference to the window to establish View Context-->
<RelativeSource x:Key="View" Mode="FindAncestor"
AncestorType="{x:Type Window}" />
<ResourceDictionary.MergedDictionaries>
<!--custom control-->
<ResourceDictionary Source="pack://application:,,,/ContentToggleButton;component/Themes/Generic.xaml" />
<!--Local Style-->
<ResourceDictionary Source="pack://application:,,,/ButtonStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
在 MainWindow.xaml...
<Window x:Class="Spec.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:b="clr-namespace:ContentToggleButton;assembly=ContentToggleButton"
Title="MainWindow" Height="350" Width="525">
<Grid>
<b:ContentToggle Name="Toggle" Height="30"
Content="{Binding options, RelativeSource={StaticResource View}}"
Style="{DynamicResource LocalButtonStyle}"
/>
</Grid>
</Window>
在 ButtonStyle.xaml...
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!--Custom Button backgrounds-->
<LinearGradientBrush x:Key="Button.Static.Background"
EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF2C0606" Offset="1"/>
<GradientStop Color="#E6ADAD"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="Button.MouseOver.Background" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF2C0606" Offset="1"/>
<GradientStop Color="#FFF2F2"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="Button.MouseOver.Checked.Background" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF2C0606" Offset="1"/>
<GradientStop Color="#F2FFF3"/>
</LinearGradientBrush>
<LinearGradientBrush x:Key="Button.Checked.Background" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF2C0606" Offset="1"/>
<GradientStop x:Name="GradientStop" Color="#ADE6B1"/>
</LinearGradientBrush>
<!--Establish the style colours-->
<SolidColorBrush x:Key="Button.Static.Border" Color="#FF707070" />
<SolidColorBrush x:Key="Button.MouseOver.Border" Color="#FF3C7FB1" />
<SolidColorBrush x:Key="Button.Pressed.Background" Color="#C4F6CE" />
<SolidColorBrush x:Key="Button.Pressed.Border" Color="#FF2C628B" />
<SolidColorBrush x:Key="Button.Disabled.Background" Color="#FFF4F4F4" />
<SolidColorBrush x:Key="Button.Disabled.Border" Color="#FFADB2B5" />
<SolidColorBrush x:Key="Button.Disabled.Foreground" Color="#FF838383" />
<!--Custom Style-->
<Style x:Key="LocalButtonStyle" TargetType="{x:Type ButtonBase}" BasedOn="{StaticResource ButtonStyle}">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="Background"
Value="{StaticResource Button.Static.Background}"/>
<Setter Property="BorderBrush"
Value="{StaticResource Button.Static.Border}"/>
</Style>
</ResourceDictionary>
解决方案结构(两种情况通用)
【问题讨论】:
-
你在哪里使用
ContentToggle控制? -
您的
ContentToggle控件派生自ButtonBase? -
@AnjumSKhan:是的,基类是
ToggleButton -
@AnjumSKhan:感谢您的链接,不幸的是它对我没有帮助。从我添加的图片中可以看出,样式已经在 /Themes/Generic.xaml 中,所以我已经有了推荐的结构。与链接案例和我的不同之处在于它们在控件库中创建子类并导出它们。我在消费应用程序中导出基类和子类,因此没有可以放置在 Generic.xaml 文件中的资源。
标签: c# wpf xaml custom-controls