View previous topic :: View next topic |
Author |
Message |
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Fri Mar 25, 2016 4:54 pm Post subject: Combining a Resx binding with ResourceEnumConverter |
|
|
Today I came across a scenario I haven't run into before, and Globalizer didn't respond the way I expected it to. I think I understand why it's doing what it's doing, but I'm wondering if there's a workaround that I'm not seeing. I think I can fallback to a normal binding with a converter, but thought I would ask here first.
The scenario is this: I have a .Net 4.5 WPF application using the Infralution.Localization.WPF library. I have a property of a custom enum type that I want to bind on the view, so I created a ResourceEnumConverter and added the appropriate values to my resx file. I know that the type converter is working, because I can just bind the enum property directly to a TextBlock and I see the appropriate values from the resx on the screen.
What I want to do however is not just bind the property value, I want to have it parameterized into some other fixed text. So I created another key in the resx file with a value like: "{0} foo bar", and then used a <Resx> element for the Text property pointing to that Key and the BindingPath set to the enum property.
It looks like when you do this, the type converter doesn't get invoked for the enum value. Instead what I'm seeing when I run this is the enum value as a string inserted into the rest of the text from the resx value.
Is there a way I'm not seeing to combine these two ideas like this, or do I need to just do a regular binding with a custom value converter?
Thanks,
Kevin |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Mar 28, 2016 11:46 am Post subject: |
|
|
We will take a look and see if it is possible to support using the TypeConverter within a binding format string like that. If you could email support@infralution.com a small sample project (zipped) that demonstrates exactly what is happening that would help us with investigation (perhaps modify one of the sample projects). _________________ Infralution Support |
|
Back to top |
|
|
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Mon Apr 11, 2016 1:12 pm Post subject: |
|
|
Sorry, it took me a while to circle back around to this. I avoided the issue by using a normal ValueConverter instead. However, I still think this is a potentially valuable use case if it can be made to work. So I went ahead and put together a sample project to illustrate what I'm talking about. I'll email it to the support address now.
Thanks,
Kevin |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Thu Apr 14, 2016 7:28 am Post subject: |
|
|
Thanks for the sample project it made clear what you wanted to do. You can actually already do what you want with the current release by using the BindingConverter property. First you need to change your ColorsTypeConverter definition so that it has a parameterless constructor like:
Code: | public class ColorsTypeConverter : ResourceEnumConverter
{
public ColorsTypeConverter()
: base (typeof(Colors), MainWindowResources.ResourceManager) { }
}
|
Next in your XAML you need to a namespace definition for your code and define the type converter as a static resources like this:
Code: | <Window x:Class="ResxWithTypeConverter.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:src="clr-namespace:ResxWithTypeConverter"
ResxExtension.DefaultResxName="ResxWithTypeConverter.MainWindowResources">
<Grid>
<Grid.Resources>
<src:ColorsTypeConverter x:Key="colorsTypeConverter"/>
</Grid.Resources>
|
Finally you set your Resx definition to use the BindingConverter like:
Code: | <TextBlock.Text>
<Resx Key="CurrentColorDescription"
BindingPath="CurrentColor"
BindingConverter='{StaticResource colorsTypeConverter}'/>
</TextBlock.Text>
|
Now when you run the code the type converter is used to convert the colors enum to a string in the Binding. Note that because the StaticResource references a type defined in the current project you may need to compile the project and then reload the project before the XAML editor will stop complaining about spurious errors. _________________ Infralution Support |
|
Back to top |
|
|
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Thu Apr 14, 2016 1:24 pm Post subject: |
|
|
Ah, very interesting. Thanks for pointing that out, I'll give it a try. I didn't realize you could use a TypeConverter in that way ... I assumed the BindingConverter was only for an IValueConverter, like you would use on a normal binding. |
|
Back to top |
|
|
|