0

我正在编写针对 WinRT 的第一个自定义用户控件,但遇到了问题。

我想在我的控件中公开一个图像 PART_NwBadge 和它作为依赖属性的可见性。然后我想通过样式中的设置器提供默认值。这部分不起作用。而是应用 DependencyProperty(在 BadgedButton.cs 中)的默认值。

甚至可以做我所描述的吗?还是应该在 C# 代码中设置默认值?如果我确实需要在 C# 代码中设置值,有人会评论如何在代码中加载图像资源吗?经过大量搜索,我还没有找到可行的解决方案。

最后,由于这是我第一次认真尝试编写自定义控件,请提出我可以做出的任何改进,即使它们与问题没有直接关系。

Windows 8 Consumer Preview
C#/WinRT/Metro
Visual Studio 11 Beta

主题/Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="using:InkSdkTestApplication.Controls">

    <Style TargetType="l:BadgedButton">
        <Setter Property="Width" Value="36"/>
        <Setter Property="Height" Value="36"/>
        <Setter Property="Background" Value="#1C1C1C"/>
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="BorderThickness" Value="2"/>
        <Setter Property="NwBadge">
            <Setter.Value>
                <Image Width="16" Height="16" Source="../Assets/mouse_16x16.png"/>            
            </Setter.Value>
        </Setter>
        <Setter Property="NwBadgeVisibility" Value="Collapsed"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="l:BadgedButton">
                    <Border x:Name="PART_Border"
                            Width="{TemplateBinding Width}"
                            Height="{TemplateBinding Height}"
                            Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">

                        <Grid>
                            <ContentPresenter x:Name="PART_Content"
                                              HorizontalAlignment="Center"
                                              VerticalAlignment="Center"
                                              Content="{TemplateBinding Content}"/>

                            <Image x:Name="PART_NwBadge"
                                   HorizontalAlignment="Left"
                                   VerticalAlignment="Top"
                                   Width="16" Height="16"
                                   Visibility="{TemplateBinding NwBadgeVisibility}"
                                   Source="{TemplateBinding NwBadge}"/>
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

控件/BadgedButton.cs

namespace InkSdkTestApplication.Controls
{
    public sealed class BadgedButton : Control
    {
        #region // Dependency Properties
        public static DependencyProperty ContentProperty =
            DependencyProperty.Register(
                "Content",
                typeof(FrameworkElement),
                typeof(BadgedButton),
                new PropertyMetadata(null));

        public static DependencyProperty NwBadgeProperty =
            DependencyProperty.Register(
                "NwBadge",
                typeof(Image),
                typeof(BadgedButton),
                new PropertyMetadata(null));

        public static DependencyProperty NwBadgeVisibilityProperty =
            DependencyProperty.Register(
                "NwBadgeVisibility",
                typeof(Visibility),
                typeof(BadgedButton),
                new PropertyMetadata(Visibility.Visible));
        #endregion

        #region // Public Properties
        public FrameworkElement Content
        {
            get { return (FrameworkElement)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public Image NwBadge
        {
            get { return (Image)GetValue(NwBadgeProperty); }
            set { SetValue(NwBadgeProperty, value); }
        }

        public Visibility NwBadgeVisibility
        {
            get { return (Visibility)GetValue(NwBadgeVisibilityProperty); }
            set { SetValue(NwBadgeVisibilityProperty, value); }
        }
        #endregion

        public BadgedButton()
        {
            this.DefaultStyleKey = typeof(BadgedButton);
        }
    }
}
4

1 回答 1