I have following XAML in my WPF Control:
App.Xaml
<Application x:Class="MyWPFApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:miscellaneous="clr-namespace:MyWPFApp.Miscellaneous"
Startup="OnAppStart">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:CustomColorTheme BaseTheme="Light" PrimaryColor="#FF0080FF" SecondaryColor="White" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/materialdesigntheme.Flipper.xaml" />
<ResourceDictionary Source="Converters/Converters.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This renders the text box without validation error below, when I run the application and type something in the text box:
XAML 1
<Grid>
<TextBox
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
VerticalAlignment="Center"
md:TextFieldAssist.HasClearButton="True"
md:HintAssist.Hint="Email Address"
md:HintAssist.HelperText="{Binding ErrorContent}">
<TextBox.Text>
<Binding
UpdateSourceTrigger="PropertyChanged"
ValidatesOnNotifyDataErrors="True">
<Binding.Path>Email</Binding.Path>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
But when I changed the XAML as follows (Grid enclosed in a MaterialDesign.DialogHost element):
XAML 2
<md:DialogHost>
<Grid>
<TextBox
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
VerticalAlignment="Center"
md:TextFieldAssist.HasClearButton="True"
md:HintAssist.Hint="Email Address"
md:HintAssist.HelperText="{Binding ErrorContent}">
<TextBox.Text>
<Binding
UpdateSourceTrigger="PropertyChanged"
ValidatesOnNotifyDataErrors="True">
<Binding.Path>Email</Binding.Path>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</md:DialogHost>
The rendering is exactly what I was expecting:
My question is, shouldn't be the rendering like in Figure. 2 with XAML 1?
Yes, it should provided that there is an AdornerDecorator somewhere above the Grid in the element tree.
Putting your sample markup in a default Window works just fine, i.e. the error message is display as expected.
This is all there is in App.xaml:
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme BaseTheme="Dark" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Using MaterialDesignThemes 3.1.3.
Related
I want to style an activation indicator from material design in WPF. I want the styling to happen in an external styling.xaml file, so the textbox I'm using can be binded to that style.
The textbox:
<TextBox materialDesign:HintAssist.Hint="Search:" Grid.Column="1" Margin="35 8 20 5"
Width="200" Style="{"STYLE style"}/>
You could set it as a StaticResource:
File: styling.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:YourNamespace">
<Style TargetType="{x:Type TextBox}" x:key="YourCustomTextBoxStyle">
<Setter Property="Margin" Value="35 8 20 5"/>
....
</Style>
</ResourceDictionary>
In your View (or Application) add a reference to the styling ResourceDictionary and use the style as a static resource:
<UserControl
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:local="clr-namespace:YourNamespace.Wpf"
x:Class="YourView" mc:Ignorable="d">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourNamespace;component/Themes/styling.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<TextBox materialDesign:HintAssist.Hint="Search:" Grid.Column="1" Width="200" Style="{StaticResource YourCustomTextBoxStyle}/>
</UserControl>
Im new at WPF and im using some DevExpress controls
Im trying to implement an style to some buttons but always shows the error that the Resource cannot be resolved.
MainWindow:
<dx:DXWindow
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core" dx:ThemeManager.ThemeName="Office2016"
Title="MainWindow" Height="746.218" Width="1139.154"
WindowStartupLocation="CenterScreen">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="80"/>
</Grid.RowDefinitions>
<StackPanel x:Name="Stack_Top" Orientation="Horizontal" Grid.Row="1" >
<dx:SimpleButton x:Name="btnRefresh" Style="{StaticResource ResourceKey=CustomStyles}" Width="55" ToolTip="Refresh" Margin="10,10,10,10" Glyph="{dx:DXImage Image=Refresh_32x32.png}" Content="Resfresh" />
<dx:SimpleButton x:Name="btndNew" Width="55" ToolTip="New Customer" Margin="10" Glyph="{dx:DXImage Image=NewContact_32x32.png}" Content="New Customer" />
<dx:SimpleButton x:Name="btnDelete" ToolTip="Delete Customer" Width="55" Margin="10" Content="Delete" Glyph="{dx:DXImage Image=Cancel_32x32.png}"/>
</StackPanel>
</Grid>
This is the App.xaml
<Application x:Class="LicenceManagerWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="OnAppStartup_UpdateThemeName">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary x:Name="CustomStyles" Source="StyleResource.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This is my Styles file
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LicenceManagerWPF"
xmlns:dxb="http://schemas.devexpress.com/winfx/2008/xaml/bars"
xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" x:Class="LicenceManagerWPF.MainWindow"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Style x:Name="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
I was searching and everything looks like fine but i dont get it why it cannot be resolved.
Regards
There is no need to name ResourceDictionary , just provide its Source:
<ResourceDictionary Source="StyleResource.xaml"/>
ResourceDictionary items should have x:Key. In case of a Style if there is no explicit key, TargetType will be used as a key. It is a way to create default styles.
If you want named style, then set x:Key
<Style x:Key="HeaderButtons" TargetType="dx:SimpleButton">
<Setter Property="Margin" Value="10"/>
<Setter Property="Width" Value="55"/>
</Style>
And finally StaticResource extension references resources by resource key, not by names:
Style="{StaticResource HeaderButtons}"
Also: when you set a Style for a button, Margin and Width settings (Width="55" Margin="10,10,10,10") becomes redundant. They can be used to override style setting, but in this case they are the same so why write them?
I am creating a simple WPF Gui. I am trying to apply implicit styles to all UserControls to give them some default appearances - the background color, and font. This seems to work on the user control itself. However, when the user control is embedded in a Window the styles are list.
For example, here is a user control:
Code:
<UserControl x:Class="NtpClient.Gui.Components.MainPanel.MainPanelView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:serverSelector="clr-namespace:NtpClient.Gui.Components.ServerSelector"
xmlns:detailsPanel="clr-namespace:NtpClient.Gui.Components.DetailsPanel"
Height="350" Width="400">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<serverSelector:ServerSelectorView Grid.Column="0" Padding="6" />
<detailsPanel:DetailsPanelView Grid.Column="1" Padding="6" />
</Grid>
</UserControl>
And here is the Window with the UserControl in:
Code:
<Window x:Class="NtpClient.Gui.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mainPanel="clr-namespace:NtpClient.Gui.Components.MainPanel"
Title="Network Time Servers" Height="350" Width="400">
<mainPanel:MainPanelView Width="auto" Height="auto"/>
</Window>
The Themes file is set in app.xaml and looks like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes_CoreThemes;component/ControlStyles.xaml" />
<ResourceDictionary Source="/Themes_CoreThemes;component/BrushResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- Implicit Styles -->
<Style BasedOn="{StaticResource DeepBlueButtonStyle}" TargetType="Button" />
<Style BasedOn="{StaticResource ControlStyle}" TargetType="UserControl" />
</ResourceDictionary>
App.xaml:
<Application x:Class="NtpClient.Gui.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Themes_CoreThemes;component/ControlStyles.xaml" />
<ResourceDictionary Source="/Themes_CoreThemes;component/BrushResources.xaml" />
<ResourceDictionary Source="Themes/Local/Local.MSControls.Core.Implicit.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Does anyone have any idea what is causing this, and how I resolve it?
I've faces that problem some month's ago.
It's because your xaml files are in other dll's.
I don't know why, but designer isn't handling that properly.
Look like it's external issue of Designer.
I've just moved styles from dll's to the main project.
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 datagrid in silverlight 4 like below
<data:DataGrid x:Name="Test">
<data:dataGrid.Columns>
<data:DataGridColumnTextColumn Header="File Name" HeaderStyle="{StaticResource MyResource}"/>
</data:DataGrid.Columns>
</data:DataGrid>
Here's the Resource file property
<Style TargetType ="System_Windows_Controls_Primitives:DataGridColumnHeader" x:Name="MyResource">
<Setter Property="Foreground" Value="#FF"/>
</style>
the xmlns I use is xmlns:data = "clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" - This was working in Silverlight 3 but not in Silverlight 4
The issue is that the program is throwing an error that the given key is not present in the dictionary which is not true since it is there in the resource file. It can clearly get other keys for other properties like Header Name etc but not the HeaderStyle . Can someone tell me what I am doing wrong here.
Thanks
Simply creating a dictionary does not make your application incorporate it. Did you reference it in your App.xaml?
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SilverlightApplication5.App">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
[Edit]
I'm not sure then. Are the namespaces correct?
The below example works for me (note namespace names differ from your example):
MainPage.xaml:
<UserControl
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:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"
x:Class="SilverlightApplication5.MainPage"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<sdk:DataGrid HorizontalAlignment="Left" Height="100" Margin="120,126,0,0" VerticalAlignment="Top" Width="120">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="File Name" HeaderStyle="{StaticResource MyResource}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</Grid>
Dictionary1.xaml:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">
<Style TargetType="sdk:DataGridColumnHeader" x:Name="MyResource">
<Setter Property="Foreground" Value="#FFFFFFFF"/>
</Style>
App.xaml is exactly as posted above.
Ok, I think I spotted the error:
You mistakenly have used the attribute x:Name and not x:Key, but you have to set the x:Key attribute for static resources. Yes, I have seen the x:Name actually working for resources before (seems Silverlight 3 wasn't too strict about it) and I think you can't do this for SL4 anymore.
<Style TargetType="DataGridColumnHeader" x:Name="MyResource">
versus
<Style TargetType="DataGridColumnHeader" x:Key="MyResource">