问题
I'm trying to create a simple checkbox, which is an image instead of the classic one. I managed to create the control and assign it different behaviours, but I can't make it to work when checking/unchecking. Here's the code :
<UserControl.Resources>
<ControlTemplate x:Key="CheckboxImageTemplate" TargetType="CheckBox">
<Image Name="imgTreble" MinWidth="100" Source="Images/treble_unchecked.png"/>
</ControlTemplate>
</UserControl.Resources>
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneForegroundBrush}">
<CheckBox Height="72" HorizontalAlignment="Left" x:Name="checkBox1" Background="White" VerticalAlignment="Top" Template="{StaticResource CheckboxImageTemplate}" >
<Custom:Interaction.Triggers>
<Custom:EventTrigger EventName="Checked">
<ic:ChangePropertyAction PropertyName="Source" TargetName="imgTreble" Value="Images/treble_checked.png"/>
</Custom:EventTrigger>
<Custom:EventTrigger EventName="Unchecked">
<ic:ChangePropertyAction PropertyName="Source" TargetName="imgTreble" Value="Images/treble_unchecked.png"/>
</Custom:EventTrigger>
</Custom:Interaction.Triggers>
</CheckBox>
</StackPanel>
I think that the problem's that I'm trying to change a property for one of the control's (in this case, the checkbox) controltemplate's items (the image). There's probably a problem with TargetName, and I should be referencing in a different way how to tell the EventTrigger to look for the image and change its source, but I don't know how Any help is appreciated
回答1:
For that purpose you need to use VisualStates in your checkbox's ControlTemplate.
Here is a very good article that customizes a checkbox to act as toggle button.
WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
You can use the same method to present images for each visual state.
回答2:
Rather than doing this, you should retemplate (set a custom style on) the checkbox to achieve your desired output.
In the custom style, use the "Checked" VisualState to hide and show the appropriate images.
来源:https://stackoverflow.com/questions/6667916/windows-phone-7-image-checkbox