0

我正在開發一個Windows 8應用程序。我想定製單選按鈕。我想使用圖像進行檢查&未經檢查的狀態。我想使用附加屬性在&上關閉內容。下面給出的是我自定義樣式代碼&附加屬性。我正在關注Template Binding with Attached Properties。問題是我收到各種錯誤。如何將框架元素屬性與附加屬性綁定?

如果我在XAML中放置/關閉圖片URL,我會得到異常。

enter image description here

如果我給你開/關圖像URL從代碼隱藏然後我得到UnhandledException &它的信息是「無法分配財產‘Windows.UI.Xaml.Data.Binding.Path’ [行:59職位:84]

自定義單選按鈕風格

<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)); 
} 

回答