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
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 would like to have the equivalent of the XAML, but in C# code:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
<SolidColorBrush x:Key="SystemControlHighlightBaseMediumLowBrush" Color="White" />
<SolidColorBrush x:Key="SystemControlHighlightBaseHighBrush" Color="White" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
</ResourceDictionary>
</Application.Resources>
Add the following to your Application OnLaunched method:
ResourceDictionary lightTheme = new ResourceDictionary();
lightTheme["SystemControlHighlightBaseMediumLowBrush"] = new SolidColorBrush(Windows.UI.Colors.White);
lightTheme["SystemControlHighlightBaseHighBrush"] = new SolidColorBrush(Windows.UI.Colors.White);
App.Current.Resources.ThemeDictionaries.Add("Light", lightTheme);
You can use
Brush SystemControlHighlightBaseHighBrush = new SolidColorBrush(Colors.White);
Brush SystemControlHighlightBaseHighBrush = new SolidColorBrush(Colors.White);
Then you could use it like this to set the colour of a button
myButton.Background = SystemControlHighlightBaseHighBrush;
If, as your comment suggests you want to change the colour on mouse hover you need to capture the mousehover event and then you could change the colour to one of your named styles. For example to change the button colour on mousehover you could do this
private void myButton_MouseHover(object sender, System.EventArgs e)
{
myButton.Background = SystemControlHighlightBaseHighBrush;
}
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);
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