Infralution Support Forum Index Infralution Support
Support groups for Infralution products
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Combining a Resx binding with ResourceEnumConverter

 
Post new topic   Reply to topic    Infralution Support Forum Index -> Globalizer Support
View previous topic :: View next topic  
Author Message
kevinkuebler



Joined: 19 Jan 2015
Posts: 20
Location: United States

PostPosted: Fri Mar 25, 2016 4:54 pm    Post subject: Combining a Resx binding with ResourceEnumConverter Reply with quote

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
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 4920

PostPosted: Mon Mar 28, 2016 11:46 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kevinkuebler



Joined: 19 Jan 2015
Posts: 20
Location: United States

PostPosted: Mon Apr 11, 2016 1:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
Infralution



Joined: 28 Feb 2005
Posts: 4920

PostPosted: Thu Apr 14, 2016 7:28 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
kevinkuebler



Joined: 19 Jan 2015
Posts: 20
Location: United States

PostPosted: Thu Apr 14, 2016 1:24 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Infralution Support Forum Index -> Globalizer Support All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group