如何将框架元素属性与附加属性绑定?

时间:2013-12-10 12:45:42

标签: windows-8 windows-runtime microsoft-metro windows-store-apps winrt-xaml

我正在开发一个Windows 8应用程序。我想定制radiobutton。我想用图像来检查&未经检查的状态。我想使用附加属性来设置&关闭内容。下面给出的是我的自定义风格和代码的代码。附属物。我关注Template Binding with Attached Properties。问题是我遇到了各种错误。

如果我在XAML中打开/关闭图像URL,我就会遇到异常。

enter image description here

如果我从代码隐藏中分配开/关图像网址,那么我将获得UnhandledException&它的消息是“无法分配给属性'Windows.UI.Xaml.Data.Binding.Path'。[行:59位置:84]

自定义RadioButton样式

<Style x:Key="AppBarRadioButtonStyle" TargetType="RadioButton">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="{StaticResource RadioButtonContentForegroundThemeBrush}"/>
    <Setter Property="Padding" Value="1,4,0,0"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="VerticalAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Left"/>
    <Setter Property="VerticalContentAlignment" Value="Top"/>
    <Setter Property="FontFamily" Value="{StaticResource ContentControlThemeFontFamily}"/>
    <Setter Property="FontSize" Value="{StaticResource ControlContentThemeFontSize}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="RadioButton">
                <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver"/>
                            <VisualState x:Name="Pressed"/>
                            <VisualState x:Name="Disabled"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="CheckStates">
                            <VisualState x:Name="Checked">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="OnImage"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unchecked"/>
                            <VisualState x:Name="Indeterminate"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualWhite"/>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="FocusVisualBlack"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unfocused"/>
                            <VisualState x:Name="PointerFocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Grid VerticalAlignment="Top">
                            <Image x:Name="OffImage" Stretch="None" Source="{Binding Path=(local:ImageRadio.OffImage),RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Image x:Name="OnImage" Stretch="None" Opacity="0" Source="{Binding Path=(local:ImageRadio.OnImage),RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Rectangle x:Name="FocusVisualWhite" Height="29" Opacity="0" StrokeDashOffset="1.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualWhiteStrokeThemeBrush}" StrokeDashArray="1,1" Width="29"/>
                            <Rectangle x:Name="FocusVisualBlack" Height="29" Opacity="0" StrokeDashOffset="0.5" StrokeEndLineCap="Square" Stroke="{StaticResource FocusVisualBlackStrokeThemeBrush}" StrokeDashArray="1,1" Width="29"/>
                        </Grid>
                        <ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" Grid.Column="1" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<RadioButton x:Name="rdo1" GroupName="F" Style="{StaticResource AppBarRadioButtonStyle}" 
             local:ImageRadio.OffImage="ms-appx:///images/archives1.png"
             local:ImageRadio.OnImage="ms-appx:///images/archives2.png"/>
class ImageRadio
{
    public static Uri GetOnImage(DependencyObject obj)
    {
        return (Uri)obj.GetValue(OnImageProperty);
    }

    public static void SetOnImage(DependencyObject obj, Uri value)
    {
        obj.SetValue(OnImageProperty, value);
    }

    public static readonly DependencyProperty OnImageProperty =
        DependencyProperty.RegisterAttached("OnImage", typeof(Uri), typeof(ImageRadio), new PropertyMetadata(null));

    public static Uri GetOffImage(DependencyObject obj)
    {
        return (Uri)obj.GetValue(OffImageProperty);
    }

    public static void SetOffImage(DependencyObject obj, Uri value)
    {
        obj.SetValue(OffImageProperty, value);
    }

    public static readonly DependencyProperty OffImageProperty =
        DependencyProperty.RegisterAttached("OffImage", typeof(Uri), typeof(ImageRadio), new PropertyMetadata(null));
}

1 个答案:

答案 0 :(得分:2)

How to use Attached property within a style?帮助了我。可能标记类ImageRadio public解决了问题。

还将依赖项属性类型从Uri更改为string