Reputation: 15296
I am developing a Windows 8 app. I want to customize radiobutton. I want to use images for checked & unchecked states. I want to use attached properties to set on & off content. Below given is my code for custom style & attached properties. I am following Template Binding with Attached Properties. The problem is I am getting various errors.
If I put on/off image URL in XAML, I am getting below exception.
If I assign on/off image URL from code-behind then I am getting UnhandledException
& it's message is "Failed to assign to property 'Windows.UI.Xaml.Data.Binding.Path'. [Line: 59 Position: 84]"
Custom RadioButton style
<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));
}
Upvotes: 0
Views: 288
Reputation: 15296
How to use Attached property within a style? helped me. May be marking class ImageRadio
public
solved the problem.
Also changed dependency property type from Uri
to string
Upvotes: 2