I have a Windows Forms UserControl which I am converting to WPF. It works just fine to start, but I need to have the button images change depending on the toggle state. I thought it would be simple to create a UserControl with a ToggleButton containing an Image. I then changed the UserControl with the burttosn so that the first button would use the ImageToggleButton. All appears to work, except the image never displays in either checked state.
Here is the XAML for the top usercontrol:
<controls:ImageToggleButton x:Name="b_LayerFreezeThaw" Grid.Column="0" Margin="5"
CheckedImage ="{StaticResource LayerFrozen}"
UncheckedImage ="{StaticResource LayerThawed}"
Style="{StaticResource SquareButton}" />
<!--<Button x:Name="b_LayerFreezeThaw" Grid.Column="0" Margin="5" Content="{StaticResource LayerThawed}" Click="b_LayerFreezeThaw_Click" Background="{x:Null}"/>-->
<Button x:Name="b_LayerOnOff" Grid.Column="1" Margin="5" Content="{StaticResource LayerOn}" Click="b_LayerOnOff_Click" Background="{x:Null}"/>
<Button x:Name="b_LayerLockUnlock" Grid.Column="2" Margin="5" Content="{StaticResource LayerUnLocked}" Click="b_LayerLockUnlock_Click" Background="{x:Null}"/>
<Button x:Name="b_LayerPlot" Grid.Column="3" Margin="5" Content="{StaticResource LayerPlotOn}" Click="b_LayerPlot_Click" Background="{x:Null}"/>
<Button x:Name="b_LayerNewVP" Grid.Column="4" Margin="5" Content="{StaticResource NewVPThawed}" Click="b_LayerNewVP_Click" Background="{x:Null}"/>
And the XAML for the ImageTogglebutton:
<ToggleButton x:Name="ToggleBtn" Background="Transparent" Checked="ToggleBtn_Checked" Unchecked="ToggleBtn_Unchecked">
<Image x:Name='ButtonImage'/>
</ToggleButton>
And now the codebehind for the toggle:
public partial class ImageToggleButton : UserControl
{
public ImageToggleButton()
{
InitializeComponent();
DataContext = this;
}
/// <summary>
/// Set image to show on checked.
/// </summary>
public Image CheckedImage
{
get
{
return (Image)GetValue( CheckedImageProperty);
}
set
{
SetValue( CheckedImageProperty, value);
}
}
static readonly DependencyProperty CheckedImageProperty
= DependencyProperty
.Register("CheckedImage",
typeof(Image
),
typeof(ImageToggleButton
),
new PropertyMetadata
(null));
/// <summary>
/// Set image to show on unchecked.
/// </summary>
public Image UncheckedImage
{
get
{
return (Image)GetValue(UncheckedImageProperty);
}
set
{
SetValue(UncheckedImageProperty, value);
}
}
static readonly DependencyProperty UncheckedImageProperty
= DependencyProperty
.Register("UncheckedImage",
typeof(Image
),
typeof(ImageToggleButton
),
new PropertyMetadata
(null));
private void ToggleBtn_Checked(object sender, RoutedEventArgs e)
{
ButtonImage = CheckedImage;
}
private void ToggleBtn_Unchecked(object sender, RoutedEventArgs e)
{
ButtonImage = UncheckedImage;
}
}
When debugging everything seems to be functioning except that the Images which are supposed to be passed as the UnserControl properties are never set. They are specifically set in the UserControl containing the Toggle UC but the Set function is not hit when the window is initiated.
Suggestions on how to correct this most welcome.