C# Wpf Binding type adapter2019 Community Moderator ElectionHow do I calculate someone's age in C#?What is the difference between String and string in C#?Cast int to enum in C#How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What are the correct version numbers for C#?How to enable assembly bind failure logging (Fusion) in .NETWhy is Dictionary preferred over Hashtable in C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?
Why are there no stars visible in cislunar space?
Worshiping one God at a time?
Can you move over difficult terrain with only 5 feet of movement?
Bash - pair each line of file
What does Jesus mean regarding "Raca," and "you fool?" - is he contrasting them?
Loading the leaflet Map in Lightning Web Component
Recruiter wants very extensive technical details about all of my previous work
Why is there so much iron?
What (if any) is the reason to buy in small local stores?
Using Past-Perfect interchangeably with the Past Continuous
HP P840 HDD RAID 5 many strange drive failures
How to define limit operations in general topological spaces? Are nets able to do this?
What should I install to correct "ld: cannot find -lgbm and -linput" so that I can compile a Rust program?
What does Deadpool mean by "left the house in that shirt"?
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
Can other pieces capture a threatening piece and prevent a checkmate?
How to generate binary array whose elements with values 1 are randomly drawn
What favor did Moody owe Dumbledore?
gerund and noun applications
How is the partial sum of a geometric sequence calculated?
Hausdorff dimension of the boundary of fibres of Lipschitz maps
I seem to dance, I am not a dancer. Who am I?
Is there a hypothetical scenario that would make Earth uninhabitable for humans, but not for (the majority of) other animals?
Is there a term for accumulated dirt on the outside of your hands and feet?
C# Wpf Binding type adapter
2019 Community Moderator ElectionHow do I calculate someone's age in C#?What is the difference between String and string in C#?Cast int to enum in C#How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?What are the correct version numbers for C#?How to enable assembly bind failure logging (Fusion) in .NETWhy is Dictionary preferred over Hashtable in C#?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How to loop through all enum values in C#?
As of now, i assign the image of a TreeView item using a direct binding to the image's source:
<DataTemplate DataType="x:Type local:GeoPoint">
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
the Color binding is referring to string containing the path to the PNG, something like "/Resources/red.png"
i would like to make the Color variable of custom type "MarkerColor", an enum containing several colors, and have the image source binding reference this value, so that if
Color = MarkerColor.green; the binding would reference "/Resources/green.png"
Note that the name of the PNG is not necessarily the same as the name of MarkerColor, an "adapter" should be used to convert the type
I know how to do this in Java Android SDK, but not really sure on how to achive this in Wpf
c# .net wpf binding
add a comment |
As of now, i assign the image of a TreeView item using a direct binding to the image's source:
<DataTemplate DataType="x:Type local:GeoPoint">
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
the Color binding is referring to string containing the path to the PNG, something like "/Resources/red.png"
i would like to make the Color variable of custom type "MarkerColor", an enum containing several colors, and have the image source binding reference this value, so that if
Color = MarkerColor.green; the binding would reference "/Resources/green.png"
Note that the name of the PNG is not necessarily the same as the name of MarkerColor, an "adapter" should be used to convert the type
I know how to do this in Java Android SDK, but not really sure on how to achive this in Wpf
c# .net wpf binding
add a comment |
As of now, i assign the image of a TreeView item using a direct binding to the image's source:
<DataTemplate DataType="x:Type local:GeoPoint">
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
the Color binding is referring to string containing the path to the PNG, something like "/Resources/red.png"
i would like to make the Color variable of custom type "MarkerColor", an enum containing several colors, and have the image source binding reference this value, so that if
Color = MarkerColor.green; the binding would reference "/Resources/green.png"
Note that the name of the PNG is not necessarily the same as the name of MarkerColor, an "adapter" should be used to convert the type
I know how to do this in Java Android SDK, but not really sure on how to achive this in Wpf
c# .net wpf binding
As of now, i assign the image of a TreeView item using a direct binding to the image's source:
<DataTemplate DataType="x:Type local:GeoPoint">
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
the Color binding is referring to string containing the path to the PNG, something like "/Resources/red.png"
i would like to make the Color variable of custom type "MarkerColor", an enum containing several colors, and have the image source binding reference this value, so that if
Color = MarkerColor.green; the binding would reference "/Resources/green.png"
Note that the name of the PNG is not necessarily the same as the name of MarkerColor, an "adapter" should be used to convert the type
I know how to do this in Java Android SDK, but not really sure on how to achive this in Wpf
c# .net wpf binding
c# .net wpf binding
asked Mar 6 at 22:04
user26302user26302
234
234
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could create a converter that knows how to convert the enumeration value to a valid resource:
public class ColorResourceConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
MarkerColor color = (MarkerColor)value;
Uri uri;
switch(color)
case MarkerColor.Green:
uri = new Uri("Resources/green.png");
break;
case MarkerColor.Red:
uri = new Uri("Resources/red.png");
break;
//...
default:
uri = new Uri("Resources/default.png");
break;
return new BitmapImage(uri);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotSupportedException();
Usage:
<DataTemplate DataType="x:Type local:GeoPoint">
<DataTemplate.Resources>
<local:ColorResourceConverter x:Key="ColorResourceConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color, Converter=StaticResource ColorResourceConverter" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032895%2fc-sharp-wpf-binding-type-adapter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could create a converter that knows how to convert the enumeration value to a valid resource:
public class ColorResourceConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
MarkerColor color = (MarkerColor)value;
Uri uri;
switch(color)
case MarkerColor.Green:
uri = new Uri("Resources/green.png");
break;
case MarkerColor.Red:
uri = new Uri("Resources/red.png");
break;
//...
default:
uri = new Uri("Resources/default.png");
break;
return new BitmapImage(uri);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotSupportedException();
Usage:
<DataTemplate DataType="x:Type local:GeoPoint">
<DataTemplate.Resources>
<local:ColorResourceConverter x:Key="ColorResourceConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color, Converter=StaticResource ColorResourceConverter" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
add a comment |
You could create a converter that knows how to convert the enumeration value to a valid resource:
public class ColorResourceConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
MarkerColor color = (MarkerColor)value;
Uri uri;
switch(color)
case MarkerColor.Green:
uri = new Uri("Resources/green.png");
break;
case MarkerColor.Red:
uri = new Uri("Resources/red.png");
break;
//...
default:
uri = new Uri("Resources/default.png");
break;
return new BitmapImage(uri);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotSupportedException();
Usage:
<DataTemplate DataType="x:Type local:GeoPoint">
<DataTemplate.Resources>
<local:ColorResourceConverter x:Key="ColorResourceConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color, Converter=StaticResource ColorResourceConverter" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
add a comment |
You could create a converter that knows how to convert the enumeration value to a valid resource:
public class ColorResourceConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
MarkerColor color = (MarkerColor)value;
Uri uri;
switch(color)
case MarkerColor.Green:
uri = new Uri("Resources/green.png");
break;
case MarkerColor.Red:
uri = new Uri("Resources/red.png");
break;
//...
default:
uri = new Uri("Resources/default.png");
break;
return new BitmapImage(uri);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotSupportedException();
Usage:
<DataTemplate DataType="x:Type local:GeoPoint">
<DataTemplate.Resources>
<local:ColorResourceConverter x:Key="ColorResourceConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color, Converter=StaticResource ColorResourceConverter" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
You could create a converter that knows how to convert the enumeration value to a valid resource:
public class ColorResourceConverter : IValueConverter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
MarkerColor color = (MarkerColor)value;
Uri uri;
switch(color)
case MarkerColor.Green:
uri = new Uri("Resources/green.png");
break;
case MarkerColor.Red:
uri = new Uri("Resources/red.png");
break;
//...
default:
uri = new Uri("Resources/default.png");
break;
return new BitmapImage(uri);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
throw new NotSupportedException();
Usage:
<DataTemplate DataType="x:Type local:GeoPoint">
<DataTemplate.Resources>
<local:ColorResourceConverter x:Key="ColorResourceConverter" />
</DataTemplate.Resources>
<StackPanel Orientation="Horizontal">
<Image Source="Binding Color, Converter=StaticResource ColorResourceConverter" Height="32" />
<TextBlock Text="Binding Name" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
answered Mar 8 at 13:46
mm8mm8
86.5k81933
86.5k81933
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55032895%2fc-sharp-wpf-binding-type-adapter%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown