Referencing DrawingImage in a binding.
-
I'm not certain what I have to google for. I have Drawing images in an resource file like this : I can use them like this : Now, I want to be able to bind the image source and conditionally choose the image in the Code-Behind. I'm not certain how I reference the image in the code ? Is this a DrawingImage ? How do I load it in the code ? public DrawingImage ChooseIcon { get { if (useFindIcon) { return ???; // how to reference the static resource "FindIcon" } else if (useSearchIcon) { return ???;// how to reference the static resource "SearchIcon" } } } Thanks. Max.
CI/CD = Continuous Impediment/Continuous Despair
-
I'm not certain what I have to google for. I have Drawing images in an resource file like this : I can use them like this : Now, I want to be able to bind the image source and conditionally choose the image in the Code-Behind. I'm not certain how I reference the image in the code ? Is this a DrawingImage ? How do I load it in the code ? public DrawingImage ChooseIcon { get { if (useFindIcon) { return ???; // how to reference the static resource "FindIcon" } else if (useSearchIcon) { return ???;// how to reference the static resource "SearchIcon" } } } Thanks. Max.
CI/CD = Continuous Impediment/Continuous Despair
The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:
public enum ChooseIcon
{
Find,
Search,
}public class YourViewModel
{
private ChooseIcon _chooseIcon;public ChooseIcon ChooseIcon { get { return \_chooseIcon; } set { SetProperty(ref \_chooseIcon, value); } }
}
public class ChooseIconConverter : IValueConverter
{
public ImageSource FindIcon { get; set; }
public ImageSource SearchIcon { get; set; }public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { switch ((ChooseIcon)value) { case ChooseIcon.Find: return FindIcon; case ChooseIcon.Search: return SearchIcon; default: return null; } } public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Binding.DoNothing; }
}
<Window.Resources>
<local:ChooseIconConverter
x:Key="ChooseIconConverter"
FindIcon="{StaticResource FindIcon}"
SearchIcon="{StaticResource SearchIcon}"
/>
</Window.Resources>
...
<Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:
public enum ChooseIcon
{
Find,
Search,
}public class YourViewModel
{
private ChooseIcon _chooseIcon;public ChooseIcon ChooseIcon { get { return \_chooseIcon; } set { SetProperty(ref \_chooseIcon, value); } }
}
public class ChooseIconConverter : IValueConverter
{
public ImageSource FindIcon { get; set; }
public ImageSource SearchIcon { get; set; }public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { switch ((ChooseIcon)value) { case ChooseIcon.Find: return FindIcon; case ChooseIcon.Search: return SearchIcon; default: return null; } } public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Binding.DoNothing; }
}
<Window.Resources>
<local:ChooseIconConverter
x:Key="ChooseIconConverter"
FindIcon="{StaticResource FindIcon}"
SearchIcon="{StaticResource SearchIcon}"
/>
</Window.Resources>
...
<Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks. Will look at that. I also bodge a hack by having 2 Image tags and add a Visibility binding.
CI/CD = Continuous Impediment/Continuous Despair
-
The view-model shouldn't really know anything about your resources. I'd suggest having an enum to represent the icon you want to display. Bind the source to that enum, and use a converter to convert the value to the relevant image. Eg:
public enum ChooseIcon
{
Find,
Search,
}public class YourViewModel
{
private ChooseIcon _chooseIcon;public ChooseIcon ChooseIcon { get { return \_chooseIcon; } set { SetProperty(ref \_chooseIcon, value); } }
}
public class ChooseIconConverter : IValueConverter
{
public ImageSource FindIcon { get; set; }
public ImageSource SearchIcon { get; set; }public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { switch ((ChooseIcon)value) { case ChooseIcon.Find: return FindIcon; case ChooseIcon.Search: return SearchIcon; default: return null; } } public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return Binding.DoNothing; }
}
<Window.Resources>
<local:ChooseIconConverter
x:Key="ChooseIconConverter"
FindIcon="{StaticResource FindIcon}"
SearchIcon="{StaticResource SearchIcon}"
/>
</Window.Resources>
...
<Image Source="{Binding ChooseIcon, Mode=OneWay, Converter={StaticResource ChooseIconConverter}}" />
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Awesome, it's working. :rose:
CI/CD = Continuous Impediment/Continuous Despair