I am making a menu that can display many languages so I want to set the Uid of these TextBlock on each item, how can I set the Uid of a TextBlock in XAML through data binding. Because when I set x:Uid="{Binding Label}", the IDE shows that is an error. Please help me, thank you!
The binding is used Text="{Binding Label}", when you want value to be fetched from viewmodel. Drawback you will have to handle according to language the conversion of text.
x:Uid is supposed to be specified inside Strings->en_US folder. You create a resource file by default Resources.resw file inside it you add Name lets say Sample.Text and its Value as Your Text.
Inside your XAMl you are supposed to use x:Uid="Sample".
Now as per your requirement when you add more language using multilingual toolkit it would handle conversion for required language and on uploading to store it would select the particular language from app package and hence user views the app in the particular language they have selected on their device.
If you want to use the translation from strings folders you can instead of binding to the x:Uid value, bind the Text variable to a text key and use a value converter and find the translated text in code behind.
So the Xaml will look something like:
<TextBlock Text="{Binding Label, Converter={StaticResource TranslatorConverter}}"/>
Then you need the value converter:
public class TranslatorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var resourceLoader = Windows.ApplicationModel.Resources.ResourceLoader.GetForCurrentView();
return resourceLoader.GetString(((String)value)); ;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
The "Label" in your Resources.resw file shall then simply be without the ".text" part that you use when using the x:Uid binding.
However if the multilingual toolkit as suggested is easier or not I donĀ“t know. I have not used the multilingual toolikt.
Related
In the auto-generated resource designer file, there are properties for each resource. The property calls "GetString" which returns the string value. I would like to override this getstring function so I can do logic to see if I need to retrieve this value or a different value. I can't figure out how to do this because the designer file is auto-generated.
Public ReadOnly Property General() As String
Get
Return ResourceManager.GetString("General", resourceCulture)
End Get
End Property
For example, in my version of the GetString function, I would check the key passed in ("General") and see if there is a custom value for this key in a database. If the custom value exists, I would use that value. If the custom value does not exist, I would call the base GetString function to get the Resource value. I'd like to use the built in Resource class for this because then in my code I can just use "#Resources.General" and take advantage of the auto-complete functionality that already exists.
Refer to ASP.NET Resourcemanager to read local .resx. It's in C# but you can just convert it over. It isn't 100% of what you are looking for but shows a way of overriding in which you may be able to adjust to work with your needs.
I have an observable collection of objects in my VM. I want to bind to a property of a specific item in the list in a text block, something like this:
Binding="{MyVMCollection[0].Description}"
But this syntax does not work. Is it possible to do what I am after and if so, how?
Thanks!
You're missing the Binding keyword and I think you also need to use Path.
Binding="{Binding Path=MyVMCollection[0].Description}"
The type of the object needs to be a type where an array index would normally work for this to work. I'm not sure the exact constraints but use Type[] if in doubt.
eg. If it is some weird enumerable type like IOrderedEnumerable<T> (or some wierd LINQy type) then something like {Binding List[0]} won't work.
I'm trying to implement this using Eugene Akinshin's code from here: http://forums.silverlight.net/t/237947.aspx/1
It seems like a really nice way to bind to already-existing properties and means the configuration can all be defined in XAML.
However, I can't get it to work.
I'm defining the templates to use like this:
<Converters:TemplateSelectorConverter x:Key="templateConverter">
<Converters:TemplateSelectorCase TemplateReference="Minimised" Template="{StaticResource SmallOrders}"/>
<Converters:TemplateSelectorCase TemplateReference="Restored" Template="{StaticResource MediumOrders}"/>
<Converters:TemplateSelectorCase TemplateReference="Maximised" Template="{StaticResource LargeOrders}"/>
</Converters:TemplateSelectorConverter>
and then setting the item template of my ListBox like this:
ItemTemplate="{Binding CurrentState, Converter={StaticResource templateConverter}}"
CurrentState is a string of either 'Minimised', 'Maximised' or 'Restored' (I've edited the linked example to have a string as the key rather than an int) and is set to 'Minimised' by default, but all I get is a list of Cannot create [my object type] in my ListBox.
The templates definitely work as I can expose the View in the ViewModel and set the DataTemplate in code, and there are visual states that rely on the same CurrentState property which work, so I know the View can access the property correctly. Unfortunately, if I breakpoint the Convert() method in the converter, it never gets hit.
All suggestions greatly appreciated!
Not sure if you have figured this out or not but placement of the Converter definition in the Resource in relationship to the DataTemplate will be the difference between it working and not working.
The Converter needs to be placed prior to the Data Template.
I have a custom silverlight control, which exposes a property with DataGridLength type. Now I want that property to have the same editor as a common DataGridColumn's Width property, with the combobox and everything, like this:
instead, I only get a simple TextBox, with "Auto" written in, with no way to set to SizeToCells and so on.
I assume I need a DesignTime attribute, but none of the ones I found in ComponentModel namespace even came close...
I guess you just have to create an Enum with the all the values autorized (Pixels, SizeToCells, etc ...), you that Enum as the type of your property DataGridLength and then, in the code of your control, take the corresponding action regarding the value sent.
I think the easiest way to explain this is by example.
I have a Datagrid with a data context of a list of People Objects:
People{
string Name;
int AstroSignCode;
}
I am using a code to store the astro sign because the values will be persisted in a database. I cant just use the astrology Object.
Then I have a Text Column which binds to this object and uses a converter which returns an AstrologySign Object from a static list of signs based on a cross reference between AstrologySign.ID and People.AstroSignCode:
AstrologySign{
string Name;
DateTime StartDate;
DateTime EndDate;
int ID;
}
So my converter is returning an Object instead of something displayable.
How do I bind the Column to the Member of the object returned from the converter?
my Xaml so far for the column is this:
I think that I might need to use a DataGridTemplateColumn but I'm not sure anymore.
I answered my own question. it took some time to figure this out because to me, it was not obvious.
I did infact need to use a DataGridTemplateColumn instead of a DataGridTextColumn.
<sdk:DataGridTemplateColumn Header="Astro Sign">
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock DataContext="{Binding AstroSign, Converter={StaticResource AstrologySignConverter}}" Text="{Binding Name}"/>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>
What is happening here is the data context of the DataGrid is a list of People. Each Person has an astrosigncode which is an integer. This gets mapped by the AstrologySignConverter to an AstrologySign object which Contains the name. so I make a datatemplate and use the TextBlock control and i set it's context to a binding using the converter then i bind to the element of the object that the converter returns.
It's simple once you see that you can do it. Thanks anybody who tried to solve this!