I had asked this question the other day. Now, I'm trying to apply the same practice to make all my windows, and have CenterScreen as it's default WindowStartupLocation. I've tried typing:
<Style TargetType="Window">
<Setter Property="WindowStartupLocation">
</Setter>
</Style>
However, apparently WindowStartupLocation isn't a supported property for this. Is there a way to accomplish this that I'm missing, or am I going to have to manually change this for all windows?
App.xaml
<Application.Resources>
<ResourceDictionary>
<Style x:Key="WindowStyle" TargetType="Window">
<Setter Property="SizeToContent" Value="WidthAndHeight" />
<Setter Property="ResizeMode" Value="CanMinimize" />
</Style>
<WindowStartupLocation x:Key="WSL">CenterOwner</WindowStartupLocation>
</ResourceDictionary>
</Application.Resources>
Window
<Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="525" Height="350" WindowStartupLocation="{StaticResource WSL}" Style="{StaticResource WindowStyle}">
<Window.Resources />
<Grid />
</Window>
Related
I am having an issue while I want to apply a style (font size) to all my textbox, where ever they are.
I, of course found those links :
Apply an application-level style to all textboxes
How to target all controls (WPF Styles)
But this doesn't work for me.
The first one is still better because you don't need to use a key, and going to all your textbox.
I already have, in my app.xml, style applying to all controls (applying a color), so I tried something like this (even if this won't really be enough for me, since those styles are not everywhere) :
<Style x:Key="Type1Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="#FEE9E6"/>
<Style.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
</Style.Resources>
</Style>
And as I said, I tried this too, but won't work.
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="50" />
</Style>
Any idea what is my probleme and how I can achieve what I want to?
All the hings I can find make me come back to the same code, and didn't found one that works.
edit : here is my current app.xaml
<Application x:Class="myApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:myApp"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="TextBox"><!-- tried adding those 3 lines too-->
<Setter Property="FontSize" Value="50"/>
</Style>
<Style x:Key="Type1Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Blue"/>
</Style>
<Style x:Key="Type2Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="White"/>
</Style>
<Style x:Key="Type3Data" TargetType="{x:Type Control}">
<Setter Property="Background" Value="Green"/>
</Style>
</Application.Resources>
</Application>
As I said, the current styles does not cover the whole app (I add the key that is needed, or nothing)
edit : and if I add directly (as a second setter) font size to the Type 1,2 or 3, the font size apply. So it seems, there is no oter styles that are applying besides the 3 in app.xml.
<Setter Property="FontSize" Value="50" />
Sample of code who should get a different textbox size(one with already a style, one without), they are in grid:
<com:ViewControl x:Class="myApp.View.ViewControl"
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:syncfusion="http://schemas.syncfusion.com/wpf"
xmlns:input="http://schemas.syncfusion.com/wpf"
xmlns:local="clr-namespace:myApp.View"
xmlns:com="clr-namespace:RAPINF.PLL.Common;assembly=myApp.Common"
xmlns:entities="clr-namespace:myApp.Entities;assembly=myApp.Entities"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="700">
<TextBox Style="{StaticResource Type1Data}" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" />
<TextBox Grid.Row="0" Grid.Column="1" Margin="2" Text="{Binding Data.Name}" VerticalAlignment="Center" Grid.ColumnSpan="3" />
edit : adding the code of the popup I use sometimes and works
<sf:RibbonWindow x:Class="namespace:myApp.Common.DetailViewWindow"
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:sf="http://schemas.syncfusion.com/wpf"
xmlns:self="clr-namespace:myApp.Common"
mc:Ignorable="d"
d:Height="300" d:Width="400"
WindowStartupLocation="CenterOwner"
>
</sf:RibbonWindow>
How my usercontrol is added to the dock :
public void AddView(UserControl View, string sTitle, DockState docState)
{
int Width = 800;
int Height = 400;
DockingManager.SetHeader(View, sTitle);
DocumentContainer.SetMDIBounds(View, new Rect(30, 30, Width, Height));
DockingManager.SetState(View, docState);
DockingManager.SetShowCloseMenuItem(View, true);
DockingManager.SetDesiredWidthInDockedMode(View, Width);
DockManager.Children.Add(View);
ActivateView(View);
}
If I use the dock I do this before :
ApplicationContext.Current.AddView(View, DockState.Document);
ANd with a popup, almost the same :
DetailViewWindow dlg = new DetailViewWindow(View);
dlg.ShowDialog();
As, those two code are used with the same View (yes, exactly the same), then I guess the problem comes from the fact of adding the view in the dock, and not in a popup window.
Does the dock manager forces me to use a key?
Thank you for your help.
I can't reproduce your problem. I can just show you a working example. Maybe it's helping you finding your problem.
App.xaml
<Application x:Class="WpfApplication2.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication2"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize"
Value="50" />
</Style>
</Application.Resources>
</Application>
MainWindow.xaml
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication2"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow"
Name="MyWindow"
SizeToContent="WidthAndHeight">
<TextBox Width="150"/>
</Window>
Output
I too had no problem with the styling. But I typically break my stuff into separate dictionaries for different controls and derive from some of my own classes. I also define a style and assign a "x:Key" name to it. This is like saying I have a class with this key name and I want it to look like this. Then, once I get it working, I take the final class and say use your style based on the key defined... Having said that, Here is a stripped basic of the App.xaml
<Application.Resources>
<Style TargetType="TextBox" x:Key="STextBox">
<Setter Property="FontSize" Value="50"/>
<Setter Property="FontFamily" Value="WingDings" />
</Style>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource STextBox}" />
</Application.Resources>
I can define all sorts of things about my base-style "STextBox" (via the x:Key). But after, I set the style with a target BASED ON the style via x:Key. The critical thing I think you were missing is the target must be of {x:Type TextBox}.
If you look at the xmlns headers at the top such as
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
the "x=" is the alias of the library it is getting control types. By you generically declaring the
TargetType="TextBox"
You are not specifically associating to the CLASS TYPE.
Then in my main window (and anywhere else in the app), I have this in my main grid
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80" />
<RowDefinition Height="80" />
<RowDefinition Height="80" />
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Testing" />
<TextBox Grid.Row="1" Text="Another Line" />
<TextBox Grid.Row="2" Text="Last" />
</Grid>
and they all work properly. You can also change default colors, size, font family, margins, etc to the base style and all will change without explicit reference to every control.
I am following all steps listed on this link,
https://modernuicharts.codeplex.com/documentation
but it gives below error and only title and subtitle show up no chart
Severity Code Description Project File Line Suppression State
Error 'ChartBase' TargetType does not match type of element
'PieChart'. MteroChartsModern c:\users\sesa388372\documents\visual
studio
2015\Projects\MteroChartsModern\MteroChartsModern\Application.xaml 9
My xaml code:
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<chart:PieChart
Style="{StaticResource MinimalChartStyle}"
ChartTitle="Minimal Pie Chart"
ChartSubTitle="Chart with fixed width and height"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
<chart:PieChart.Series>
<chart:ChartSeries
SeriesTitle="Errors"
DisplayMember="Category"
ValueMember="Number"
ItemsSource="{Binding Path=Errors}" />
</chart:PieChart.Series>
</chart:PieChart>
</Grid> </Window>
Application.xaml
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary >
<Style x:Key="MinimalChartStyle" TargetType="chart:ChartBase">
<Setter Property="Width" Value="500"/>
<Setter Property="Height" Value="500"/>
</Style>
</ResourceDictionary>
</Application.Resources>
The issue is caused since you are defining the Style for TargetType="chart:ChartBase" and what you are using is chart:PieChart.
Please define a style for chart:PieChart like :
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary >
<Style x:Key="MinimalChartStyle" TargetType="chart:PieChart">
<Setter Property="Width" Value="500"/>
<Setter Property="Height" Value="500"/>
</Style>
</ResourceDictionary>
</Application.Resources>
I’m just testing some XAML stuff, and I am wondering why a style which comes from a resource dictionary is created multiple times when used inside a data template for listbox.
Let me give you an example
<UserControl x:Class="WpfApplication1.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ListBox ItemTemplate="{DynamicResource foo}" ItemsSource="{Binding Names}"/>
</UserControl>
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication1="clr-namespace:WpfApplication1">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
<DataTemplate x:Key="foo">
<TextBlock Style="{StaticResource TextBlockStyle}" Text="{Binding}" Loaded="TextBlock_Loaded"/>
</DataTemplate>
</ResourceDictionary>
</Window.Resources>
<wpfApplication1:UserControl1 />
</Window>
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="TextBlockStyle" TargetType="TextBlock">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Pink" />
</Style>
</ResourceDictionary>
Now I’ve done a simple test in code behind to see whether the textblocks get the same style,
and it is surprising that each one of them gets a new instance of the same style, so if I had 50 records then I would have 50 instances of the same style.
Is that how people do it, or is there a pattern to reuse the same instance as many times as needed?
Thanks for looking :)
I have a UserControl that consists of a listview it looks like:
<UserControl
....
<UserControl.Resources>
<Style TargetType="Thumb">
<!-- Style Content -->
</Style>
<Style TargetType="GridViewColumnHeader">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ScrollBar}">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ScrollViewer}">
<!-- Style Content -->
</Style>
<Style TargetType="{x:Type ListViewItem}">
<!-- Style Content -->
</Style>
</UserControl.Resources>
<ListView Name="ListView1" >
<!-- ListViewContent -->
</Style>
</UserControl>
I have 3 of those userControls where the only thing that is different between them is the styles in <UserControl.Resources>. It makes no scene to have to create multiple controls that have the same functionality just because I need a different look and feel. What I want to do now is combine all the styles in <UserControl.Resources> into one style. If I manage to group all those styles into one I would be able to remove the 3 controls and change the style as:
<ListView Style={DynamicResource style1} ...
Currently if I do
<UserControl.Resources>
<Style x:Key="style1">
<!-- Place all styles in here -->
</Style>
</UserControl.Resources>
It does not work.
Edit
Thanks to iltzortz answer I now have:
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid">
<Setter Property="Background" Value="Green"></Setter>
</Style>
<SolidColorBrush x:Key="Foo" Color="Red"></SolidColorBrush>
</ResourceDictionary>
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="Grid">
<Setter Property="Background" Value="Black"></Setter>
</Style>
<SolidColorBrush x:Key="Foo" Color="Orange"></SolidColorBrush>
</ResourceDictionary>
MyUserControl:
<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"
mc:Ignorable="d"
d:DesignHeight="97" d:DesignWidth="91">
<UserControl.Resources>
<ResourceDictionary Source="Dictionary1.xaml" ></ResourceDictionary>
</UserControl.Resources>
<Grid >
<Ellipse Fill="{DynamicResource Foo}" />
</Grid>
</UserControl>
And I change resource dictionaries dynamically like this: switching wpf resource dictionaries at runtime
Add a resource dictionary to your application named e.g. common.xaml
and put your common styles there
then you can reuse it with:
<UserControl.Resources>
<ResourceDictionary Source="common.xaml"/>
</UserControl.Resources>
You can create 3 resource dictionaries and merge them at runtime. In my example code I used two resource dictionaries.
Example:
Dictionary1.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Margin" Value="0,10,0,0" />
</Style>
</ResourceDictionary>
Dictionary2.xaml:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="btnStyle" TargetType="Button">
<Setter Property="Margin" Value="50,50,0,0" />
</Style>
</ResourceDictionary>
In the start of application you can set default style in App.xaml file:
<Application x:Class="Example.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If you want to change style, you can merge resource dictionaries:
ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("\\Dictionary2.xaml", UriKind.Relative);
this.Resources.MergedDictionaries.Add(dict);
And now binding looks like this:
<Button Style="{DynamicResource btnStyle}" Content="Click me!" />
Now if you invoke code to merge resource dictionaries, button style will be automatically changed.
I want every button to have 5 points margin, in addition to Royale theme style.
Window1.xaml:
<Window x:Class="_styles.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<Button Content="Button A"/>
<Button Content="Button B"/>
</StackPanel>
</Window>
It compiles but I get:
An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll
public Window1() {
InitializeComponent(); // <-- getting exception here
}
There are no exception details because:
{Cannot evaluate expression because the current thread is in a stack overflow state.}
This seems to be a circular reference between your style and the one defined in PresentationFramework.Royale. A workaroud would be to place resources at different levels:
<Window x:Class="_styles.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/royale.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<StackPanel>
<StackPanel.Resources>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
<Setter Property="Margin" Value="5"/>
</Style>
</StackPanel.Resources>
<Button Content="Button A"/>
</StackPanel>
</Window>
I would remove the BasedOn attribute - it's not necessary. Think of it this way, merging the Royale theme will apply the button theme, and you just want to change the margin - styles are additive in nature, so it will combine the Royale theme and your own button theme without specifying the BasedOn attribute - does that make sense?
Cheers!
Please see this question and my answer for another solution that doesn't require you to specify a resource dictionary in every window and allows you to resolve the BasedOn style dynamically.