0

So I am trying to style some buttons in XAML and running into an issue with the staticresource for a binding.

I am trying to get the button to have a Grid system, with 2 cols. Image on the left, and contentpresenter on the right (or the text). I want the image to be different for each instance of this button, so the Source on the image needs to be different. I may be approaching this the wrong way but the only way I could think of to get the button XAML to reflect a different image inside the control template was to use the Tag property of the button, set it to the source string. Then use an IValueConverter to convert the string to a Bitmap Image. But the binding is giving me a terrible time. I've searched all I could and can't figure it out for the life of me!

Here is the style code:

  <Style x:Key="ServerButtonTop" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="#0d5b8f" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="Width" Value="220"/>
        <Setter Property="Height" Value="60" />
        <Setter Property="VerticalAlignment" Value="Top" />
        <Setter Property="Margin" Value="20" />
        <Setter Property="FontSize" Value="20" />
        <!-- Template for the buttons -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border BorderBrush="#dddddd" BorderThickness="1" Background="{TemplateBinding Background}" CornerRadius="3">
                        <Grid Width="{TemplateBinding Width}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="50" />
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Image Width="40" Height="40" Source="{TemplateBinding Tag, Converter={StaticResource StringToImage}}" />
                            <ContentPresenter Grid.Column="1" VerticalAlignment="Center" HorizontalAlignment="Center" Name="thetext" />
                        </Grid>
                    </Border>
                    <!-- trigger for mouse over -->
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="Background" Value="#ACD9F8" />
                         </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="RenderTransform" TargetName="thetext">
                                <Setter.Value>
                                    <TranslateTransform Y="1.0" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

The issue is with line:

<Image Width="40" Height="40" Source="{TemplateBinding Tag, Converter={StaticResource StringToImage}}" />

and to finish off the XAML portion this is what im trying to achieve:

<Button Style="{StaticResource ServerButtonTop}" Content="Button Text" Tag="Resources/imagename.png" />

The whole button works perfectly, and styles and has the grid layout and everything, only that it wont display the image.

Here is the code for the IValueConverter Class

    [ValueConversion(typeof(string), typeof(BitmapImage))]
public class StringToImage : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
    {

        return new BitmapImage(new Uri((string)value));

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo info)
    {
        throw new NotImplementedException();
    }
} 

Currently the issue im getting is "The Resource 'StringToImage' could not be resolved." at one point while playing around I got an error of "IValueConvert cant convert from string".

The IValueConverter class is right next to the window class in the namespace for the same XAML window.

Daniel
  • 11
  • 1
  • Have you actually created an instance of the converter in the Window's Resources and assigned it the key `StringToImage`? – Clemens Apr 11 '15 at 21:51
  • @Clemens Thanks for pointing this out. I did put it in there but I had put it under the templates's resources instead of the window's resources. Stupid mistake. Thanks for finding it! – Daniel Apr 12 '15 at 00:28

1 Answers1

0

It's a little tricky to test this without a heap of bitmaps handy. I think that this should work:

<Image Width="40" Height="40" Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource StringToImage}}" />

Pretty good explanation of why this should work here: WPF TemplateBinding vs RelativeSource TemplatedParent

Community
  • 1
  • 1
goobering
  • 1,547
  • 2
  • 10
  • 24