I'm trying to create a button which contains an Image and a Text. But, the image can be changed by code behind at runtime. My images are png, and everyone is in my ResourceDictionary as:
<BitmapImage x:Key="iconLogIn" UriSource="/Images/Icons/lock.png" />
So, I've started with this style, with no Template or ContentTemplate.
<Style x:Key="MyTestButton" TargetType="{x:Type Button}">
<Setter Property="Height" Value="80" />
<Setter Property="Width" Value="100" />
</Style>
And my XAML is:
<Button x:Name="cmdOption1" Style="{StaticResource MyTestButton}" Margin="8" Click="cmdOption1_Click">
<Image Source="{DynamicResource iconLogIn}" />
</Button>
Then, my code behind to change image is:
cmdOption1.Content = new Image() { Source = ((BitmapImage)FindResource("iconLogOut")) };
So far, this works.
But, only holds the image, I want to place a text under the image.
So I've read this post, and HighCore's answer, option #2, might fulfill my requirement. But, now comes a new problem:
First, this is the new style, with a simple template
<Style x:Key="MyTestButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="#EEEEEE" />
<Setter Property="Foreground" Value="DarkSlateGray" />
<Setter Property="Height" Value="80" />
<Setter Property="Width" Value="100" />
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding" Value="2"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="SemiBold" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border>
<StackPanel>
<Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="50" Width="50"/>
<ContentPresenter Grid.Row="0" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True" />
</StackPanel>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Foreground" Value="DarkOrange" />
<Setter Property="OpacityMask" Value="#AA888888"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="Gray" />
<Setter Property="BorderBrush" Value="DarkGray" />
<Setter Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Style>
My new XAML is:
<Button x:Name="cmdOption1" Tag="{StaticResource iconLogIn}" Content="LOG IN" Style="{StaticResource MyTestButton}" Margin="8" Click="cmdOption1_Click" />
*Also with Tag="{DynamicResource iconLogIn}"
And my code behind to change the image and text:
cmdOption1.Tag = new Image() { Source = ((BitmapImage)FindResource("iconLogOut")) };
cmdOption1.Content = "LOG OUT";
With this, the content text changes from "LOG IN" to "LOG OUT". But the image is not shown anymore, just nothing in the place of the image, and no error or exception is thrown.
I'd like to know whats the solution, and whats happening?, why the image is just not changing, but disappeared?
Thanks in advance.