I have a default button style declared in App.xaml . It has DropShadowEffect applied. Strange thing is the DropShadowEffect is also getting applied to DatePicker
Can anyone help how to remove DropShadowEffect in DatePicker ?
Button style applying to DatePicker
6.2k Views Asked by msnitin At
2
There are 2 best solutions below
0
On
There is a much simpler way to fix this issue. You just need to undo the style settings in the DatePicker style.
For example, if you have an App.xaml which defines the default style of all buttons in your application, create a style for the DatePicker type, add a style resource to it to undo the button styling.
<!-- Default style for all Buttons in the application -->
<Style TargetType="{x:Type Button}">
<Setter Property="MinHeight" Value="25" />
<Setter Property="MinWidth" Value="85" />
</Style>
<!-- Default style for all DatePicker in the application -->
<Style TargetType="{x:Type DatePicker}">
<Style.Resources>
<!-- Reset the Button style inside the DatePicker to default values -->
<Style TargetType="{x:Type Button}">
<Setter Property="MinHeight" Value="0" />
<Setter Property="MinWidth" Value="0" />
</Style>
</Style.Resources>
</Style>
So whatever property setters you have for the button, just undo them and set them to default in the DatePicker Style.Resource.
Here is a complete App.xaml file for one of my applications:
<Application x:Class="App1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:App1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<!-- Default style settings for all controls -->
<Style TargetType="{x:Type Control}" x:Key="BaseStyle">
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
</Style>
<!-- Default style for all Labels in the application -->
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseStyle}" />
<!-- Default style for all Buttons in the application -->
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="MinHeight" Value="25" />
<Setter Property="MinWidth" Value="85" />
</Style>
<!-- Default style for all GroupBoxes in the application -->
<Style TargetType="{x:Type GroupBox}" BasedOn="{StaticResource BaseStyle}" />
<!-- Default style for all CheckBoxes in the application -->
<Style TargetType="{x:Type CheckBox}" BasedOn="{StaticResource BaseStyle}" />
<!-- Default style for all ComboBoxes in the application -->
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="MinHeight" Value="25" />
<Setter Property="MinWidth" Value="80" />
</Style>
<!-- Default style for all TextBoxes in the application -->
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}">
<Setter Property="MinHeight" Value="25" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
<!-- Default style for all DatePicker in the application -->
<Style TargetType="{x:Type DatePicker}" BasedOn="{StaticResource BaseStyle}">
<Style.Resources>
<!-- Reset the Button style inside the DatePicker to default values -->
<Style TargetType="{x:Type Button}">
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="MinHeight" Value="0" />
<Setter Property="MinWidth" Value="0" />
</Style>
</Style.Resources>
</Style>
</ResourceDictionary>
</Application.Resources>
This is because the DatePicker control uses the standard (default) button template that you have defined. I ran into the same issue with my default button style messing up the look of all my DatePickers. There are two was to fix this:
Solution 1: (not ideal)
Add a key to your default style and apply it to all the buttons in your app that should use it. This solution was out of the question for me as I specifically wanted the style to override the default. So...
Solution 2: (preferred but slightly painful way)
Override the default Button AND DatePicker styles in your App.xaml (or Styles.xaml) file by right clicking on the controls in the XAML designer -> Edit Template -> Edit a Copy...
Add a key to the newly added button style called something like DefaultWpfButtonStyle (ensuring that it won't be used by default) and then applying the style to the Button control inside the DatePicker style/template. This will ensure that the DatePicker uses the default WPF button style for the drop down button in your app instead of your default style.
Or you can just copy-paste the following code:
You should now have all your buttons in your app use your default style except for the DatePickers that use the default WPF button style.