C# WPF UserControl:自定义样式的TargetType的客户端验证?
我有一个多用途的C# WPF UserControl:自定义样式的TargetType的客户端验证?,c#,wpf,validation,xaml,user-controls,C#,Wpf,Validation,Xaml,User Controls,我有一个多用途的UserControl。 为了简单起见,我将首先向您展示控件: <UserControl x:Class="CompetitionAgent.View.UserControls.ExpandingButtonGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com
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>
因此,当在另一个窗口中使用此UserControl
时,它看起来有点像:
#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
<UserControls:ExpandingButtonGrid x:Name="ebgSchemeManager"
Style="{StaticResource ExpandingButtonGridStyle}"
PanelStyle="{StaticResource ExpandingButtonGridPanelStyle}"
ExpandButtonStyle="{StaticResource ExpandingButtonGridExpandButtonStyle}" />
当然有。您可以在setter中验证。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;
}
}
您可以验证设置的样式并引发异常(当您尝试这样做时,将在设计器中立即可见)。对于此
样式
/面板样式
/等必须是依赖属性。@Sinatr巧合的是,我刚刚收到此错误只能在DependencyObject的DependencyProperty上设置“DynamicResourceExtension”。
我必须研究一下,谢谢!您好,谢谢,我将立即尝试您的解决方案进行验证。谢谢如果C#语法有任何错误,请告诉我。我通常使用VB.net,但如果需要,我想修改答案。它可以工作,但您忘记了分号return\u buttonStyle
和setter的右括号后的分号不应出现\u buttonStyle=value;}代码>。如果您习惯于VB.net;,可以理解)@谢谢。固定的。
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;
}
}