How can I bind value from my resources files (es,en resources) to a element on page like text blocks and so on.
I've found this tutorial but in this case is using C#, I want to do it but just put some binding on the element markup not coding.
That page clearly shows how to associate controls to resources:
You need to associate every control that needs localized text with the
.resw file. You do this using the x:Uid attribute on your XAML
elements like this
<TextBlock x:Uid="Greeting" Text="" />
Have you tried it?
Related
I am new to WPf and MVVM. I have Dynamic language change functionality in my wpf application. For this functionality I have 2 Resource dictionary for each language.On Language change in UI, I am adding and removing the corresponding ResourceDictionary to the application.
With this, at a given point of time only one Resource Dictionary is available in Application resource.
As all strings are Dynamic resource, I need to now how to add a string to a dynamic resource string in xaml.
For ex: I want something like below
<Label FontWeight="Medium" Content="{DynamicResouce Name} + myText"/>
Here "Name" is a Key available in both resource dictionary as shown below.
<system:String x:Key="Name">Name</system:String>
Now, need to know how to attach "myText" to the Name(DynamicResource) in the Label Content.
You can just simply use TextBlock inside the Label
<Label>
<TextBlock>
<Run Text="Text1"/>
<Run Text="Text2"/>
</TextBlock>
</Label>
I'm working on a class library which contains several .resw resource files and pages. The question is, how to fetch the string and use it for UI component properties.
Better to show the case in another way. First, please refer to to image for the solution structure:
From the picture, "MobileReplicaBase" is a class library. Please not the two selected file. In "UIresources.resw", I defined a string resource:
And trying to use it in a button control in EdicolaPage:
<Button x:Name="mOpenBtn" Grid.Row="4" x:Uid="OpenBtn" Visibility="{x:Bind Path=type, Converter={StaticResource typeStringToVisibilityConverterForOpenButton}}" Tag="{x:Bind Path=productCode}" Click="mOpenBtn_Click"/>
But this won't work, the Content property for the button is only an empty string. What I can guess is that, the application is trying to load the "OpenBtn" resource from resource map in project "MobileReplica", which is currently the start up project in the solution.
Note: the button may be in a datatemplate in a GridView, so fetch the resource in C# code may not be a good idea.
Problem solved. Use following code to access resource in a class lib:
<Button x:Uid="/{library_name}/{resource_file_name}/{resource_name}"/>
In my case, I should use:
<Button x:Uid="/MobileReplicaBase/UIresources/OpenBtn">
To access to the resource.
I have a listview in my app (C# - UWP)
and in my DataTemplate i have a TextBlock that it Binding a text.
this is my code:
<TextBlock Text="{Binding Caption}" FontSize="11"/>
Now, how can i coloring all hashtags in the text? And clickable?
Note: all captions that binding this TextBlock is variable.
Like:
This is test #message for testing
Or
I like #German and #Russian language
I want change color #message, #German and #Russian and clickable feature in TextBlock
One option is to use a rich text box. Rich text box can render HTML like tags.
so you can have text like
<p> I am following the <a>#Russian-Language</a> <a>#azure</a> tutorials. </P>
Then anchor tags can have targets and they will be clickable. OR you can call a method on this hyperlink click.
Hope this helps you.
I made a control few months back called HashHandleTextBlock. The core concept of this is based on MarkdownTextBlock of UWP Community Toolkit.
Below is how you use controls.
<UnwantedControls:HashHandleTextBlock Text="{Binding ElementName=InputText, Path=Text}"
LinkForeground="DarkGray"
HashPrefix="https://twitter.com/hashtag/"
HandlePrefix="https://twitter.com/" />
You can also download the source from Github and modify control for your requirement.
When a named XAML element is used in a WPF application, it can be accessed from anywhere. For example:
<Grid>
<Grid>
<TreeViewItem Name="itemScreen" />
The element itemScreen will be directly accessible in MainWindow(), although it is several levels deep in the XAML hierarchy.
How does WPF enable this to work in C#?
There's a mechanism called NameScope.
https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/wpf-xaml-namescopes
Simple markup you put in a window which has no templating or styling will all have the one namescope.
If you dig through that link it will explain about styles and templates in more detail. Essentially, they have their own namescope.
This is probably as far as you want to go with an explanation at this stage but there are a couple of oddities like when you "inherit" a style using basedon.
I wouldn't worry about them just yet but throw it to the back of your mind for later.
ps
That control is a private member of your window and the name doesn't have to be unique across the entire application.
I am new to XAML and C#
I have an icon created already in a project and and I have to use this icon whenever I select one of the option from the dropdown menu.
I made a stackpanel in XAML file
<StackPanel Name="stackPanelforIcon">
</StackPanel>
In the code behind file I have different cases for the dropdown menu.
case IconOnSelect:
?????? = IconList.NewIcon;
This NewIcon is the one already created and I am using the source also for this
using IconProject.Iconlists;
On writing IconList.NewIcon I am not getting any error, it is referenced correctly.
What should I write at ?????? to reference it. Is there any other way apart from using stackPanel to include an icon
A StackPanel cannot show an icon on it's own. You need a control for it, for example an Image.
<StackPanel Name="stackPanelforIcon">
<Image x:Name=theImage" />
</StackPanel>
Then you can use your Icon in your code behind like this:
this.theImage.Source = IconList.NewIcon;
You may need to convert your value, you never said what type it actually is.
Please note that using code-behind is not the preferred way with WPF. Using MVVM is way easier and more natural working with WPF, using code-behind you will fight WPF all the way. Using MVVM, this could be:
<StackPanel Name="stackPanelforIcon">
<Image Source="{Binding CurrentImage}" />
</StackPanel>
with your ViewModel having a property called CurrentImage that you would set when you want to change it. Don't forget to implement INotifyPropertyChanged for the changes to take effect though.