Reputation: 11
I am creating a DataTemplate in code and cannot use XAML. :(
I have managed to create an image inside the Template, but only if I hardcode the path to the ico file. I would love to be able to bind that string to the item (I am using the DataTemplate on a modified ListView).
Here's my code right now:
private DataTemplate CreateDataTemplate(string binding, HorizontalAlignment alignment, bool active, bool useIcon)
{
DataTemplate dt = new DataTemplate();
FrameworkElementFactory sp = new FrameworkElementFactory(typeof(StackPanel));
sp.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
if (useIcon)
{
double size = 14.0;
BitmapImage bmp = new BitmapImage(new Uri("MyIcon.ico", UriKind.RelativeOrAbsolute));
FrameworkElementFactory icon = new FrameworkElementFactory(typeof(Image));
icon.SetValue(Image.SourceProperty, bmp);
icon.SetValue(Image.WidthProperty, size);
icon.SetValue(Image.HeightProperty, size);
icon.SetValue(Image.MarginProperty, new Thickness(0, 0, 5, 0));
sp.AppendChild(icon);
}
FrameworkElementFactory tb = new FrameworkElementFactory(typeof(TextBlock));
tb.SetBinding(TextBlock.TextProperty, new Binding(binding));
tb.SetValue(TextBlock.ForegroundProperty, (active ? Brushes.Black : Brushes.Gray));
tb.SetValue(TextBlock.TextTrimmingProperty, TextTrimming.CharacterEllipsis);
tb.SetValue(TextBlock.HorizontalAlignmentProperty, alignment);
sp.AppendChild(tb);
dt.VisualTree = sp;
return dt;
}
Thanks!
Upvotes: 1
Views: 4160
Reputation: 31
An IValueConverter
is the solution here. Mind the pack URI syntax though!
In case the image is not part of your solution your Convert
method should look like this:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return "pack://siteoforigin:,,,/" + (string)value;
}
Note that the image files need to be in the same directory as your applications binary, or a subdirectory thereof, since leading "../"
s will be stripped away automatically.
Upvotes: 0
Reputation: 185300
I suppose a ValueConverter could work.
public class StringToBitmapImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string uristring = value as string;
return new BitmapImage(new Uri(uristring, UriKind.RelativeOrAbsolute));
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
icon.SetBinding(Image.SourceProperty, new Binding(path) { Converter = new StringToBitmapImageConverter() });
Where path
is a property path which points towards the property holding an uri-string within the templated object.
Upvotes: 2