I'm working on a WPF-application which shall be multilingual.
So i followed the steps in this article and added some resource dictionaries to my project.
Then i add one of these dictionarioes to the window by the following method, which i'm calling - for test purposes - in the constructor of the window:
private void SetLanguageDictionary()
{
ResourceDictionary dict = new ResourceDictionary();
switch (Thread.CurrentThread.CurrentCulture.ToString())
{
case "en-US":
case "en-GB":
dict.Source = new Uri("Resources\\StringResources_en-US.xaml",
UriKind.Relative);
break;
case "de-DE":
dict.Source = new Uri("Resources\\StringResources_de-DE.xaml",
UriKind.Relative);
break;
default:
dict.Source = new Uri("Resources\\StringResources_de-DE.xaml",
UriKind.Relative);
break;
}
Resources.MergedDictionaries.Add(dict);
}
Finally i implemented the resources in the labels on my window like that:
<Label Grid.Row="0"
Grid.Column="0"
Margin="5"
Content="{DynamicResource firstname}"></Label>
If the current culture on my PC is "en-US" the content will be "First name".
Or in case of "de-DE" (german) "Vorname".
At runtime it works fine, but at designtime i can't see the texts.
What have i to do?
I have found it out.
I had to implement the ResourceDictionary in Window.Resources.
To do that, some tags have to be added ("ResourceDictionary", "ResourceDictionary.MergedDictionaries").
If there are styles defined they have to be moved inside the tag "ResourceDictionary" as You can see below.
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StringResources_en-US.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style x:Key="style1">
[...]
</Style>
[...]
</ResourceDictionary>
</Window.Resources>
Then You can see the texts from the xaml-file in the controls which are bound to it as dynamic resource.
And because it is used as dynamic resource, it can be overwritten in code-behind:
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("Resources\\StringResources_de-DE.xaml",
UriKind.Relative);
Resources.MergedDictionaries.Add(dict);
Related
I need to apply style in grid resources programmatically in code behind.
I've the following snippet of code :
<Grid x:Name="grid">
<Grid.Resources>
<Style TargetType="{x:Type ig:LabelPresenter}">
<EventSetter Event="PreviewMouseMove" Handler="LabelPresenter_PreviewMouseMove"/>
</Style>
</Grid.Resources>
.
.
.
</Grid>
I want to create the Style in code behind and add this to resources for handle the relative action.
I tried to do this in this way but it dosen't work.
public MainWindow()
{
InitializeComponent();
var style = new Style { TargetType = typeof(LabelPresenter) };
var eventSetter = new EventSetter(PreviewMouseMoveEvent, new MouseButtonEventHandler(LabelPresenter_PreviewMouseMove));
style.Setters.Add(eventSetter);
grid.Resources.Add("style", style);
}
Where I'm wrong?
Thanks in advance.
EDIT: I wrote wrong grid's name. The grid's right name is grid
The style defined in the XAML markup is implicit, i.e. it has no x:Key. So change the first argument that you are passing to the Add method to typeof(LabelPresenter).
Also, a PreviewMouseMove event handler accepts a MouseEventArgs:
var style = new Style { TargetType = typeof(LabelPresenter) };
var eventSetter = new EventSetter(PreviewMouseMoveEvent, new MouseEventHandler(LabelPresenter_PreviewMouseMove));
style.Setters.Add(eventSetter);
grid.Resources.Add(typeof(LabelPresenter), style);
I'm trying to set font size dynamically in WPF. Here what i did so far.
App.xaml
<Style TargetType="{x:Type FrameworkElement}" x:key="baseStyle"></Style>
<Style TargetType="{x:Type TextBlock}" BasedOn={StaticResource baseStyle}"/>
App.xaml.cs
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
double fontSize = System.Drawing.SystemFonts.IconTitleFont.Size;
Style style = new Style{ TargetType = typeof(FrameworkElement)};
style.Setter.Add(new Setter(TextElement.FontSizeProperty, fontSize));
Application.Current.Resources["baseStyle"] = style;
}
}
MainWindow.xaml
<TextBlock Text="This is Sparta!!!"/>
Issue:
When the OnStartup called the resource 'baseStyle' is not available. So the style is assigned to Null value due to which the style is not applied. Anyone having idea to implement it in some other way. Your help will be appreciated.
Edit:
There is one thing that i would like to clarify. In reality i have wrote that App.xaml and App.xaml.cs code in resource dictionary and merged it in App.xaml. The code written in OnStartup is written in constructor of that code behind class.
With a little modification your code works. Just remove and add the base style
Style style = new Style { TargetType = typeof(FrameworkElement) };
style.Setters.Add(new Setter(TextElement.FontSizeProperty, fontSize));
Application.Current.Resources.Remove("baseStyle");
Application.Current.Resources.Add("baseStyle" , style);
I have this style in a Resource-File:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox" x:Key="StandardTextBox"/>
<Setter Property="Foreground" Value="{StaticResource Color1}"/>
</Style>
</ResourceDictionary>
(Colors.xaml contains my brushes)
My Code to use the style:
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));
Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");
This works without a problem but it doesn't work when I dynamically add the Colors-Resource via code instead of in the TextboxStyles-File:
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));
//Adding the Colors.xaml Resource
ResourceDictionary Colors = new ResourceDictionary();
brushes.Source = (new Uri("Colors.xaml", UriKind.RelativeOrAbsolute));
TetxboxStyles.MergedDictionaries.Add(Colors);
Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");
Output-Error:
System.Windows.Markup.XamlParseException
"{DependencyProperty.UnsetValue}"
I replaced StaticResource with DynamicResource and it works
I am trying to add dynamic tabs to my application. Right now if I click a button, it will open a new page. What I want is to open this page in a new tab. But when I set up the tab content to a page , the code complains. I wanna do something like this
private void bttnGoToClientsOnClick(object sender, RoutedEventArgs e)
{
var content = new TextBlock();
TabItem tab = new TabItem();
tab.Header = "Search Page";
SearchPage sp = new SearchPage();
tab.Content = sp;
tabControl.Items.Add(tab);
this.NavigationService.Navigate(sp);
}
is there any way I can convert my page to usercontrol or cast it as user control
Thank you!
But when I set up the tab content to a page , the code complains.
It wouldn't hurt if you were more specific here :)
What is SearchPage class? It doesn't seem to be the part of the WPF framework. I googled it up on the
http://www.intersoftpt.com/ website. Is that it?
TabItem.Content needs to be of ContentControl type, which SearchPage - apparently - is not. I'm sure you need to embed this SearchPage object in some control presenter, such as a panel, before you can assign it to TabItem.Content.
Update:
Try this, then:
TabItem tab = new TabItem();
tab.Header = "Search Page";
SearchPage sp = new SearchPage();
this.NavigationService.Navigate(sp);
// ----------------------------------------------------
var frame = new Frame(); // !
frame.Navigate(sp); // !
tab.Content = frame; // !
// ----------------------------------------------------
tabControl.Items.Add(tab);
While I believe this should work, I haven't tested it. Please let me know if it doesn't do the trick.
You can always create your own UserControls, directly in the XAML definition (even if they are partial pages or windows).
In this example I assume that your SearchClass is defined in the [YourProject].Model namespace (where [YourProject] is the name of your project)
<UserControl x:Class="WpfApplication1.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:search="clr-namespace:[YourProject].Model">
<search:SearchClass>
<!--<Grid>
...ANYTHING YOU WANT HERE ! ...
</Grid>-->
</search:SearchClass>
</UserControl>
Now you can create an instance of the UserControl, even in XAML or in code-behind (remember only to declare the namespaces correctly!):
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:ctrls="clr-namespace:WpfApplication1"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<UserControl1 />
</Grid>
</Window>
...and this my code-behind...
UserControl1 myControl = new UserControl1();
I am trying to add control to ContentPresenter on then run, but control I've added does not apply theme.
Theres is code with reference to theme in xaml file:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/PocGraphDataTemplates.xaml" />
</ResourceDictionary.MergedDictionaries>
Also I've tried to set style in code behind, does not work:
this.graphLayout.Content = analyzerViewModel.AnalyzedLayout = new PocGraphLayout()
{
LayoutAlgorithmType = "FR"
};
ResourceDictionary rd = new ResourceDictionary();
rd.Source = new Uri("Resources/PocGraphDataTemplates.xaml", UriKind.Relative);
analyzerViewModel.AnalyzedLayout.Style = new Style(typeof(PocGraphLayout));
analyzerViewModel.AnalyzedLayout.Style.Resources.MergedDictionaries.Add(rd);
When control was static everything worked fine:
<ViewModel:PocGraphLayout x:Name="graphLayout"
Graph="{Binding Path=Graph}"
LayoutAlgorithmType="{Binding Path=LayoutAlgorithmType}"
Sample:LayoutManager.ManagedLayout="True"
OverlapRemovalAlgorithmType="FSA"
HighlightAlgorithmType="Simple" />
Any ideas?
PS. I am newbie in wpf.
just see the style applied are using DynamicResource instead of StaticResource