样式中的用户控件属性

发布于 2024-10-06 09:59:06 字数 413 浏览 2 评论 0原文

我有一个公开这样的公共属性的用户控件:

public Double ButtonImageHeight
{
   get { return imgButtonImage.Height; }
   set { imgButtonImage.Height = value; }
}

当我使用该控件时,我希望能够通过这样的样式设置该属性:

<Style x:Key="MyButtonStyle" TargetType="my:CustomButtonUserControl" >
   <Setter Property="ButtonImageHeight" Value="100" />
</Style>

我做错了什么?

谢谢

I have a usercontrol that expose a public property like this :

public Double ButtonImageHeight
{
   get { return imgButtonImage.Height; }
   set { imgButtonImage.Height = value; }
}

when I use that control, I want to be able to set that property throught a Style like that :

<Style x:Key="MyButtonStyle" TargetType="my:CustomButtonUserControl" >
   <Setter Property="ButtonImageHeight" Value="100" />
</Style>

What am I doing wrong?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

魂ガ小子 2024-10-13 09:59:06

谢谢马特,我自己刚刚找到它,但你是绝对正确的...这是我使用的确切代码,以防它可以帮助其他人(我找到的所有示例都在 WPF 上,silverlight 只是略有不同):

public static readonly DependencyProperty ButtonImageHeightProperty = DependencyProperty.Register("ButtonImageHeight", typeof(Double), typeof(CustomButtonUserControl),new PropertyMetadata(ButtonImageHeight_PropertyChanged ));

public Double ButtonImageHeight
{
   get { return (Double)GetValue(ButtonImageHeightProperty); }
   set { SetValue(ButtonImageHeightProperty, value); }
 }

private static void ButtonImageHeight_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
   ((CustomButtonUserControl)source).imgButtonImage.Height = (Double)e.NewValue;
}

thanks Matt, I just found it myself but you were absolutely right... here's the exact code I used in case it can help someone else (all the examples I found were on WPF, silverlight is just slightly different) :

public static readonly DependencyProperty ButtonImageHeightProperty = DependencyProperty.Register("ButtonImageHeight", typeof(Double), typeof(CustomButtonUserControl),new PropertyMetadata(ButtonImageHeight_PropertyChanged ));

public Double ButtonImageHeight
{
   get { return (Double)GetValue(ButtonImageHeightProperty); }
   set { SetValue(ButtonImageHeightProperty, value); }
 }

private static void ButtonImageHeight_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
   ((CustomButtonUserControl)source).imgButtonImage.Height = (Double)e.NewValue;
}
千と千尋 2024-10-13 09:59:06

该属性需要是依赖属性才能支持样式。

The property needs to be a dependency property in order to support styles.

隔岸观火 2024-10-13 09:59:06

您可以通过传递 imgButtonImage 的 Style 使其更加通用和美观,这样您就可以设置多个属性。因此,在您的用户控件中添加依赖属性,但将其设置为样式:

public static readonly DependencyProperty UseStyleProperty =
        DependencyProperty.Register("UseStyle", typeof(Style), typeof(CustomButtonUserControl), new PropertyMetadata(UseStyle_PropertyChanged));

    public Style UseStyle
    {
        get { return (Style)GetValue(UseStyleProperty); }
        set { SetValue(UseStyleProperty, value); }
    }

    private static void UseStyle_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((CustomButtonUserControl)source).imgButtonImage.Style = (Style)e.NewValue;
    }

请注意,我如何在 PropertyChanged 函数中将控件的样式设置为新样式。

然后,当我托管 UserControl 时,我可以传递 style:

<Style x:Name="MyFancyStyle" TargetType="Button" >
    <Setter Property="FontSize" Value="24" />
</Style>

<controls:MyUserControl UseStyle="{StaticResource MyFancyStyle}"  />

也可以在 VS 设计模式下工作! (这是一个奇迹

You can make it even more generic and nice by passing through a Style for your imgButtonImage, that way you can set multiple properties. So within your user control add the dependency property, but make it a Style:

public static readonly DependencyProperty UseStyleProperty =
        DependencyProperty.Register("UseStyle", typeof(Style), typeof(CustomButtonUserControl), new PropertyMetadata(UseStyle_PropertyChanged));

    public Style UseStyle
    {
        get { return (Style)GetValue(UseStyleProperty); }
        set { SetValue(UseStyleProperty, value); }
    }

    private static void UseStyle_PropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
    {
        ((CustomButtonUserControl)source).imgButtonImage.Style = (Style)e.NewValue;
    }

Notice how within the PropertyChanged function I set the style of the control to the new style.

Then when I host the UserControl I can pass through the style:

<Style x:Name="MyFancyStyle" TargetType="Button" >
    <Setter Property="FontSize" Value="24" />
</Style>

<controls:MyUserControl UseStyle="{StaticResource MyFancyStyle}"  />

works in VS design mode too! (It's a miracle)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文