I am trying to get multilingual translation("label or caption") string value for the given elementname in wpf.
For example; for elementname "txtDescription" ; my IValueConverter implementation will return with "Description" ; for another language will return different translation string(i.e. descripciĆ³n for Spanish) and the translation string will be Text=... of the same element.
I am new to wpf; I cant make it work. Is there any elegant way to do that with similiar manner as below.
<TextBlock Name="txtDescription" Text="{Binding Converter={StaticResource MultiLingualConverter} }"</TextBlock>
If this is not simple or requires more code then as an alternative sending "txtDescription" as an argument to MultiLingualConverter is acceptable but I dont now how to do that neither.
Definitely, you should build good localization support in your system, better then using converter for every string.
Until today, the best solution i have found and i almost always use it is this:
http://blogs.microsoft.co.il/tomershamam/2007/10/30/wpf-localization-on-the-fly-language-selection/
give it a try. good luck
You can pass the element name via the ConverterParameter property, this is an example of how it is used to pass a string to the converter.
However, WPF localization is more than returning different strings for different languages. You can read this article for more information and there is a Run Dialog Box example in the page to get you started.
Due to cyclical dependency restrictions you cannot reference a control inside itself or its descendants in the tree.
I would recommend you not to use converter for multilingual support. it is not a best way to do this.
got here for best way to do.
https://msdn.microsoft.com/en-us/library/ms745650(v=vs.110).aspx
Related
E2: My current solution is wrap the converter code in try block and return the dummy data instead.
In WPF you can specify that a binding should only occur at Design time. I am looking for an inverse of this functionality. But can't find anything. Does such a thing exist?
The reason for this behaviour is that I use a converter and it depends on values that are not initialized at design time.
I tried setting the FallbackValue property to something, but it is ignored.
So I end up with this:
Thanks
E1:
I have this line of code:
<TextBlock Text="{Binding AccountID, Converter={StaticResource IDToNameConv}, FallbackValue=Test}"/>
AccountID is a valid binding. I defined a converter above, that uses values that are not initilized at design time. Which causes NullReferenceException above. I'd like to see some dummy data that I provided in the FalbackValue property, but that does not work.
I'll try binding in code, but isn't there an easier way?
With just this:
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
and this:
mc:Ignorable="d" you have design time binding only. I would think that an inverse exists. But I didn't find anything.
Your IDToName converter should be modified so it can cope with whichever dependency it expects not being available. You don't say what that is, so I can't be more specific.
When that isn't there then you could make the converter just return "Test".
Or you could make it return null. I think then you could use TargetNullValue to return whatever default you prefer. This would be more obvious for any later maintenance than returning a text value from the converter.
I am trying to implement the answer in this so question
The problem is, that in xamarin forms 2 ingredients do not exist (or I have not found them yet):
Binding.IndexerName
Binding.ProvideValue()
I do not know why they do not exist. Maybe nobody has implemented them, maybe there is a technical reason why they cannot be implemented.
Can I still get the in xamarin forms?
Maybe in another way?
First, note that this answer probably doesn't work with Xamarin.Forms, or at least not with XamlC on.
If you want to get that working, your MarkupExtensions have to implement IMarkupExtension<BindingBase> instead of IMarkupExtension.
ProvideValue() is not defined in the Binding class, but in BindingExtension, but you probably won't win anything by instantiating a BindingExtension and calling ProvideValue on it versus returning the Binding directly.
The IndexerName refers to the IndexerName attribute of the Translator class. As you're not using it, the default is "Item", and you can use that hardcoded value.
I have a DevExpress dxe:TextEdit control with EditValueType="{x:Type system:Decimal}". I would like this TextEdit control to display and accept values that are 10,000 times the actual value. E.g., if someone enters 15, it should save the actual value as .0015. And if the value in the viewmodel is .0015, it should display 15.
I am new to WPF and I am not sure the best way to accomplish this. It seems like I can't do it with format strings, but I could be wrong. A value converter seems like overkill. I also thought about modifying the setter on my model object, but I don't think that would work, and it seems sort of hack-y.
I've worked with DevExpress a while ago.
As far as I can remember, you can create your own TextEdit class. (Derive from TextEditBase or even go further and derive from BaseEdit).
However, talking about over-killing, I think this is a much greater over-kill than just creating a converter, which will be a really good solution IMHO.
I would use the get/set functions to accomplish this. Perfect way of using them to translate presentation format to/from storage format.
No way to explain this issue except by example:
Say you have a custom UserControl with two DependencyPropertys, StatList Stats and string ImportantStat. The job of the user control is to display a table showing all of the values in Stats but with special visual treatment (like a pie chart) of the ImportantStat.
My instinct was to write a block of XAML that looked more or less like:
<PieChart Value="{Binding Path={Binding ImportantStat} }"/>
where the DataContext is prior set to Stats. So, the user passes in ImportantStat = "WinPercentage" and the pie chart binds to the WinPercentage Property of the stat line. But the user can just as easily pick some other Property to emphasize.
The problem (of course, you already know this, educated Stacker) is that you get an error message stating that you can't convert from Binding to string, which is what the outer Binding expects for Path. Though I haven't proven it to myself, I am guessing this is simply because Path is not a DependencyProperty.
So, is there any way to achieve my goal here? Feel free to break my assumptions in that first paragraph. Maybe, for example, ImportantStat can't be a string but must itself be a Binding.
Edit: Attempt #1 Failed
I was hoping that exposing from the code-behind a new DependencyProperty Binding ImportantStatBinding would allow me to rewrite the XAML as:
<PieChart Value="{Binding ImportantStatBinding, RelativeSource=... }"/>
...but to no avail. The indirect Binding is just stuck into Value itself with no attempts to resolve it.
My backup solution, which might be where this is headed, will be to just create the content inside the code-behind where I have access to ImportantStat directly and so can get away with a single Binding.
Far as I know, there is no way to concatenate data bindings in this way, without additional code. To put the problem more simply, we can have data binding (of course) of the form:
A --> B --> C
but you cannot have data binding of the form:
A --> B --> *A (*A indicates the target depends on the value of A)
because the relationships must be fixed.
It seems like it might be possible to create a Converter whose job is to convert a string into an arbitrary value by actually dereferencing a Binding using some additional context and that string as the property path. That sounds messy with type issues, so I chose the only other way I could think of:
I added a new DependencyProperty for the PieChart to the code behind and made sure that I constructed it at the appropriate times, so that the XAML could consume it. It's ugly, but it works. I just feel a little dead inside :) Hope someone finds this useful some day.
I've started creating a Windows Store App for Windows 8.1 and now I encountered a problem concerning localization.
I would like to display a string resource from a .resw file at design time, but every attempt to do so has failed, even though it works at runtime.
When using the x:Uid attribute, I still have to supply the Text property (i.e. for a TextBlock) and I don't like to write the text twice.
I also tried creating a property for the string on the viewmodel:
public string Title
{
get { return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title"); }
}
This is working at runtime, but at design time it is blank.
So the question is, is there a way to display resources from a .resw file in the XAML-designer?
More specifically, does the ResourceManager class allow .resw files to be read at design time?
Thanks for your help,
Lucas
Old Method
So, there are a couple of things you can do.
The first (and simplest, given that you're using x:Uid already) is to just supply the text into the Text field. The x:Uid-related value will overwrite whatever is in there.
<TextBlock Text="MyText" x:Uid="MainView_Title"/>
The second method is to use the property like you already have, and then check to see if the app is in Design Time (through a couple of different methods), then return a constant value if it is and the Resource if it is not.
public string Title
{
if(ViewModelBase.IsInDesignTimeStatic) //Mvvm Light's easy accessor
return "My Text";
return ResourceLoader.GetForCurrentView("Strings").GetString("MainView_Title");
}
Hope this helps and happy coding!
Edit: There appears to be a new way to do this, at least as of Windows 8.1.
New Method
Create a class which references a ResourceLoader (similar to the property described above).
Create an indexed property accessor which accepts a string key and return the value from the ResourceLoader.
public class LocalizedStrings
{
public string this[string key]
{
get
{
return App.ResourceLoader.GetForViewIndependentUse().GetString(key);
}
}
}
In your App.xaml, define a StaticResource of this type.
<Application.Resources>
<ResourceDictionary>
<common:LocalizedStrings x:Key="Localized"/>
</ResourceDictionary>
</Application.Resources>
Now, when you want to access your property with entry key MainView_Title, use this. It's more verbose, but it should translate both in the designer and in the app itself.
<TextBlock Text="{Binding Source={StaticResource Localized}, Path=[MainView_Title]}" />
You can shuffle it around to be a bit more readable if you'd like, such as:
<TextBlock Text="{Binding [MainView_Title], Source={StaticResource Localized}}" />
This is an old thread, but since Nate provided such an elegant solution to the problem for Win8.1 I figured I'd ask here...
After much investigation and experimentation, Nate's solution does not appear to work for UWP apps for Win10 under VS2017 Community. The LocalizedString approach works just fine at runtime, but it appears
App.ResourceLoader.GetForViewIndependentUse().GetString(key);
refuses to return anything except String.Empty during design time. I've done a lot of experimenting and things like
ResourceContext.GetForViewIndependentUse().QualifierValues
Seem to be identical between runtime (working) and design time (not working).
I was wondering if anyone has encountered this and solved it. Nate? :)