WPF UserControl:自定义样式的 TargetType 的客户端验证?
WPF UserControl: Client-side validation of TargetType for custom styles?
我有一个 UserControl
多用途。
为了简单起见,我先给大家看一下控件:
<UserControl x:Class="CompetitionAgent.View.UserControls.ExpandingButtonGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200"
Margin="0" Padding="0" Width="Auto" Height="Auto" >
<StackPanel Name="stpBody" Style="{Binding Style}">
<Button x:Name="btnExpander" Content="{Binding ExpanderButtonText}"
Style="{Binding ExpandButtonStyle}"
HorizontalAlignment="Center" Click="btnExpander_Click"
Height="25" Width="Auto" />
<StackPanel x:Name="stpButtons" Orientation="Horizontal"
Style="{Binding PanelStyle}"
Margin="0">
</StackPanel>
</StackPanel>
</UserControl>
控件 stpBody
、stpButtons
和 btnExpander
都具有受 DataContext
约束的样式。字段如下所示:
#region body styles
public Style Style { get; set; }
public Style ExpandButtonStyle { get; set; }
#endregion body styles
#region button pannel styles
public Style PanelStyle { get; set; }
public Style ButtonStyle { get; set; }
#endregion button pannel styles
所以当这个 UserControl
用于另一个 window 时,它看起来有点像这样:
<UserControls:ExpandingButtonGrid x:Name="ebgSchemeManager"
Style="{StaticResource ExpandingButtonGridStyle}"
PanelStyle="{StaticResource ExpandingButtonGridPanelStyle}"
ExpandButtonStyle="{StaticResource ExpandingButtonGridExpandButtonStyle}" />
我想知道,有没有一种方法可以验证 StaticResource
样式的 TargetTypes
,以便它们分别针对堆栈面板或按钮?
例如,样式 ExpandingButtonGridExpandButtonStyle
可能以 DockPanel
为目标,导致在运行时出现 XAML 解析异常。
更新 - 依赖对象的总结和更新
我刚刚弄清楚 DepencyObjects
是什么(万岁!)。
对于那些想知道的人,您需要注册该字段,以便可以动态分配 属性。
public static readonly DependencyProperty ExpandButtonStyleProperty =
DependencyProperty.Register("ExpandButtonStyle", typeof(Style), typeof(ExpandingButtonPanel));
public Style ExpandButtonStyle
{
get
{
return (Style)GetValue(ExpandButtonStyleProperty);
}
set
{
if (!typeof(Button).IsAssignableFrom(value.TargetType))
{
throw new ArgumentException("The target type is expected to be button");
}
SetValue(ExpandButtonStyleProperty, value);
}
}
当然有。您可以在 setter 中验证 Style.TargetType
。这也将由 WPF 设计器检查。
例如:
private Style _buttonStyle;
public Style ButtonStyle
{
get
{
return _buttonStyle;
}
set
{
if (!typeof(Button).IsAssignableFrom(value.TargetType))
{
throw new ArgumentException("The target type is expected to be Button");
}
_buttonStyle = value;
}
}
我有一个 UserControl
多用途。
为了简单起见,我先给大家看一下控件:
<UserControl x:Class="CompetitionAgent.View.UserControls.ExpandingButtonGrid"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="200"
Margin="0" Padding="0" Width="Auto" Height="Auto" >
<StackPanel Name="stpBody" Style="{Binding Style}">
<Button x:Name="btnExpander" Content="{Binding ExpanderButtonText}"
Style="{Binding ExpandButtonStyle}"
HorizontalAlignment="Center" Click="btnExpander_Click"
Height="25" Width="Auto" />
<StackPanel x:Name="stpButtons" Orientation="Horizontal"
Style="{Binding PanelStyle}"
Margin="0">
</StackPanel>
</StackPanel>
</UserControl>
控件 stpBody
、stpButtons
和 btnExpander
都具有受 DataContext
约束的样式。字段如下所示:
#region body styles
public Style Style { get; set; }
public Style ExpandButtonStyle { get; set; }
#endregion body styles
#region button pannel styles
public Style PanelStyle { get; set; }
public Style ButtonStyle { get; set; }
#endregion button pannel styles
所以当这个 UserControl
用于另一个 window 时,它看起来有点像这样:
<UserControls:ExpandingButtonGrid x:Name="ebgSchemeManager"
Style="{StaticResource ExpandingButtonGridStyle}"
PanelStyle="{StaticResource ExpandingButtonGridPanelStyle}"
ExpandButtonStyle="{StaticResource ExpandingButtonGridExpandButtonStyle}" />
我想知道,有没有一种方法可以验证 StaticResource
样式的 TargetTypes
,以便它们分别针对堆栈面板或按钮?
例如,样式 ExpandingButtonGridExpandButtonStyle
可能以 DockPanel
为目标,导致在运行时出现 XAML 解析异常。
更新 - 依赖对象的总结和更新
我刚刚弄清楚 DepencyObjects
是什么(万岁!)。
对于那些想知道的人,您需要注册该字段,以便可以动态分配 属性。
public static readonly DependencyProperty ExpandButtonStyleProperty =
DependencyProperty.Register("ExpandButtonStyle", typeof(Style), typeof(ExpandingButtonPanel));
public Style ExpandButtonStyle
{
get
{
return (Style)GetValue(ExpandButtonStyleProperty);
}
set
{
if (!typeof(Button).IsAssignableFrom(value.TargetType))
{
throw new ArgumentException("The target type is expected to be button");
}
SetValue(ExpandButtonStyleProperty, value);
}
}
当然有。您可以在 setter 中验证 Style.TargetType
。这也将由 WPF 设计器检查。
例如:
private Style _buttonStyle;
public Style ButtonStyle
{
get
{
return _buttonStyle;
}
set
{
if (!typeof(Button).IsAssignableFrom(value.TargetType))
{
throw new ArgumentException("The target type is expected to be Button");
}
_buttonStyle = value;
}
}