-2

I would like to ask, if it is possible to use property from ViewModel in different attribute than for example ItemSource and so on. Example explains the best:

<TextBlock Text="{Binding Name, Mode=TwoWay}" Grid.Column="1">
    <TextBlock.ToolTip>
        <Image VerticalAlignment="Top"  Width="auto" Height="auto" Source="Images/Doc/SomeImage.png"/>
    </TextBlock.ToolTip>
</TextBlock>

In this code I would like to use Name (which is used in first line in Binding) as a name of the image - instead of "SomeImage". The purpose of whole this is, that it is item of TreeView and I need to have image for each TextBlock in ToolTip dynamically, based on Name of the item.

So some naive solution will be something like this:

<TextBlock Text="{Binding Name, Mode=TwoWay}" Grid.Column="1">
    <TextBlock.ToolTip>
        <Image VerticalAlignment="Top"  Width="auto" Height="auto" Source="Images/Doc/{Binding Name}.png"/>
    </TextBlock.ToolTip>
</TextBlock>

I am searching for solution, how to do it in XAML only, with not touching code behind. If it is even possible?

Thanks a lot for any hint!

Horhc
  • 15
  • 6
  • 3
    The Source property [does support binding](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.image.sourceproperty?view=netframework-4.8) as it is a DependencyProperty. You could have a converter there I guess. – Corentin Pane Nov 21 '19 at 14:47
  • Your specific example is easily solved : https://stackoverflow.com/questions/21788855/binding-an-image-in-wpf-mvvm . Please pick different example or we close it as duplicate as your question is only about specific issue of binding image source. – Euphoric Nov 21 '19 at 14:50
  • @Euphoric It is a different question as this question really is about formatting the Binding it seems – Corentin Pane Nov 21 '19 at 14:52
  • @CorentinPane That is not a problem as whole URL should be in view model. – Euphoric Nov 21 '19 at 14:55
  • @CorentinPane Thank you sir, I didn't know I can use Binding in Source too, but now it is clear. – Horhc Nov 21 '19 at 14:56
  • 3
    Data binding is a feature of dependency properties, not the elements of ViewModels. All dependency properties can use data binding – Panagiotis Kanavos Nov 21 '19 at 14:56

2 Answers2

0

You can use StringFormat to format your string like you want without using a converter:

Source="{Binding Name, StringFormat=Images/Doc/{0}.png}"
Corentin Pane
  • 4,794
  • 1
  • 12
  • 29
-1

You can bind the Source property to a view model property:

Source="{Binding Path}"

...but if you want to substitute only a part of the path, you should use a converter:

<TextBlock Text="{Binding Name, Mode=TwoWay}" Grid.Column="1">
    <TextBlock.Resources>
        <local:Converter x:Key="conv" />
    </TextBlock.Resources>
    <TextBlock.ToolTip>
        <Image VerticalAlignment="Top"
               Source="{Binding Name, Converter={StaticResource conv}, ConverterParameter='{}Images/Doc/{0}.png'}" />
    </TextBlock.ToolTip>
</TextBlock>

public class Conveter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string name = value as string;
        string path = parameter as string;
        if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(path))
            return new BitmapImage(new Uri(string.Format(path, name), UriKind.Relative));

        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
mm8
  • 163,881
  • 10
  • 57
  • 88