View previous topic :: View next topic |
Author |
Message |
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Thu Nov 19, 2015 10:18 pm Post subject: TargetNullValue with Resx extension |
|
|
Is there a way to take advantage of the TargetNullValue of a binding using the Resx extension while supporting the dynamic culture switching feature of the CultureManager?
It's often times handy to specify a TargetNullValue on a binding to show an alternate value in something like a TextBlock or TextBox when the binding target value is null. It would be great to use this in conjunction with the Resx extension from Globalizer and allow that value to change when the culture is changed at runtime. But I haven't figured out a way to do it. Is this possible or any ideas for a workaround?
Thanks,
Kevin |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri Nov 20, 2015 12:40 pm Post subject: |
|
|
If you can put together a small sample project that demonstrates what you want to do (maybe by modifying one of our sample projects) then email a zipped copy to support@infralution.com. We will take a look and see if there is any way to achieve what you want. _________________ Infralution Support |
|
Back to top |
|
|
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Fri Nov 20, 2015 2:37 pm Post subject: |
|
|
I think I can describe the situation a little bit better here without a sample project. At least I'll try ...
(BTW, the code formatting feature doesn't seem to support any attributes in markup samples, so I had to replace the '<' and '>' characters with '[' and ']')
Say you have a regular string property on a ViewModel named "Description":
Code: | public string Description { get; set; } //This could be a full INPC property, doesn't matter |
In your XAML, you might want to do something like this so that you display the value of the Description property in a TextBlock, unless the value is null in which case you show some other message to the user:
Code: | [TextBlock Text={Binding Description, Mode="OneWay", TargetNullValue="No description available"}/] |
So I could use the Resx markup extension to provide that message string. This is similar to a previous question I asked about using it with the ConverterParameter property on a value converter:
Code: | [TextBlock Text={Binding Description, Mode="OneWay", TargetNullValue={Resx NoDescriptionMessage}}/] |
But like the ConverterParameter case, this won't work when switching cultures at runtime. It would be nice to be able to use the <Resx> element to achieve this by having a property, named something like BindingTargetNullValueKey, which would allow you to specify the Resx key for looking up the TargetNullValue for the binding when you recreate it. Does that make sense? If not, let me know and I'll put together a complete sample.
In the meantime, I've come up with a workaround involving a value converter that lets's me specify a resource key and the resource manager to use for looking up that key's value, which I can use with a normal binding to achieve this effect. A bit of infrastructure and extra markup when I need to use it, but it works. Would be nice to have it just built into the Resx extension though. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri Nov 20, 2015 10:49 pm Post subject: |
|
|
Quote: | the code formatting feature doesn't seem to support any attributes in markup samples, so I had to replace the '<' and '>' characters with '[' and '] |
If you check the "Disable HTML in this post" option then you can use XML elements like this:
Code: | <myattribute>
SomeCode |
Quote: | It would be nice to be able to use the <Resx> element to achieve this by having a property, named something like BindingTargetNullValueKey, which would allow you to specify the Resx key for looking up the TargetNullValue for the binding when you recreate it. Does that make sense? If not, let me know and I'll put together a complete sample. |
I think this may be possible. I will need sample to test any solution on - so it would certainly make things quicker from our end if you could put one together. _________________ Infralution Support |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Fri Nov 20, 2015 11:51 pm Post subject: |
|
|
Don't worry about a sample project - I think I can replicate the behaviour you want with one of our sample projects - and the good news is that I think that we can add this feature to the ResxExtension. I will create a new version of the Infralution.Localization.Wpf assembly and put it on our website on Monday for you to try. _________________ Infralution Support |
|
Back to top |
|
|
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Sat Nov 21, 2015 3:08 am Post subject: |
|
|
Awesome, thanks! I'll look for the update and try it out when it's ready. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sun Nov 22, 2015 10:36 pm Post subject: |
|
|
I've uploaded a new beta version (3.3.0.0) of the Infralution.Localization.Wpf assembly to our downloads page at:
www.infralution.com/downloads/Infralution.Localization.Wpf3-3-0.zip
This supports a new BindingTargetNullKey property that allows you to specify the resource key for the text to display when the binding is null. You use it like this:
Code: | <Resx Key="_selectedTextBlock.Text"
BindingElementName="_fileListBox"
BindingPath="SelectedItem"
BindingTargetNullKey="_selectedTextBlock.NullText"
BindingConverter="{StaticResource sampleEnumConverter}" />
|
In this case the "_selectedTextBlock.Text" defines the resource key for a format string used to format (non-null) bound values and the "_selectedTextBlock.NullText" is the resource key to use for a null value. If you don't need to format the bound values then you can just leave the Key attribute out ie
Code: | <Resx BindingElementName="_fileListBox"
BindingPath="SelectedItem"
BindingTargetNullKey="_selectedTextBlock.NullText"
BindingConverter="{StaticResource sampleEnumConverter}" />
|
Let me know if this works for you and we will include this in the next release and update the Code Project article as well. _________________ Infralution Support |
|
Back to top |
|
|
kevinkuebler
Joined: 19 Jan 2015 Posts: 20 Location: United States
|
Posted: Mon Nov 23, 2015 3:23 pm Post subject: |
|
|
Yep, works perfectly. That's exactly the behavior I was hoping for. Allows me to get rid of the somewhat clunky value converter I had created to work around this issue - much more elegant this way. Thanks! |
|
Back to top |
|
|
|