Skip to content

Commit 58a260f

Browse files
committed
Configurable shadows
1 parent fca1e92 commit 58a260f

File tree

9 files changed

+184
-4
lines changed

9 files changed

+184
-4
lines changed

MainDemo.Wpf/MainDemo.Wpf.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@
109109
<Compile Include="ProvingGround.xaml.cs">
110110
<DependentUpon>ProvingGround.xaml</DependentUpon>
111111
</Compile>
112+
<Compile Include="Shadows.xaml.cs">
113+
<DependentUpon>Shadows.xaml</DependentUpon>
114+
</Compile>
112115
<Compile Include="TextFields.xaml.cs">
113116
<DependentUpon>TextFields.xaml</DependentUpon>
114117
</Compile>
@@ -175,6 +178,10 @@
175178
<SubType>Designer</SubType>
176179
<Generator>MSBuild:Compile</Generator>
177180
</Page>
181+
<Page Include="Shadows.xaml">
182+
<SubType>Designer</SubType>
183+
<Generator>MSBuild:Compile</Generator>
184+
</Page>
178185
<Page Include="TextFields.xaml">
179186
<SubType>Designer</SubType>
180187
<Generator>MSBuild:Compile</Generator>

MainDemo.Wpf/MainWindow.xaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@
110110
<wpfExample:Dialogs />
111111
</domain:DemoItem.Content>
112112
</domain:DemoItem>
113+
<domain:DemoItem Name="Shadows">
114+
<domain:DemoItem.Content>
115+
<wpfExample:Shadows />
116+
</domain:DemoItem.Content>
117+
</domain:DemoItem>
113118
</ListBox>
114119
<ContentControl Margin="16" Content="{Binding ElementName=DemoItemsListBox, Path=SelectedItem.Content}" />
115120
</DockPanel>

MainDemo.Wpf/Shadows.xaml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<UserControl x:Class="MaterialDesignColors.WpfExample.Shadows"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:local="clr-namespace:MaterialDesignColors.WpfExample"
7+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
8+
mc:Ignorable="d"
9+
d:DesignHeight="300" d:DesignWidth="300">
10+
<StackPanel Margin="0 8 0 0">
11+
<TextBlock>Raised controls have default shadows set, but the shadows can be overriden.</TextBlock>
12+
<StackPanel Orientation="Horizontal" Margin="0 32 0 0">
13+
<Button Style="{DynamicResource MaterialDesignRaisedButton}"
14+
>DEPTH 1</Button>
15+
<Button Style="{DynamicResource MaterialDesignRaisedButton}"
16+
Margin="16 0 0 0"
17+
wpf:ShadowAssist.ShadowDepth="Depth2"
18+
>DEPTH 2</Button>
19+
<Button Style="{DynamicResource MaterialDesignRaisedButton}"
20+
Margin="16 0 0 0"
21+
wpf:ShadowAssist.ShadowDepth="Depth3"
22+
>DEPTH 3</Button>
23+
<Button Style="{DynamicResource MaterialDesignRaisedButton}"
24+
Margin="16 0 0 0"
25+
wpf:ShadowAssist.ShadowDepth="Depth4"
26+
>DEPTH 4</Button>
27+
<Button Style="{DynamicResource MaterialDesignRaisedButton}"
28+
Margin="16 0 0 0"
29+
wpf:ShadowAssist.ShadowDepth="Depth5"
30+
>DEPTH 5</Button>
31+
</StackPanel>
32+
<StackPanel Orientation="Horizontal" Margin="0 32 0 0">
33+
<Button Style="{DynamicResource MaterialDesignFloatingActionButton}"
34+
wpf:ShadowAssist.ShadowDepth="Depth1"
35+
>1</Button>
36+
<Button Style="{DynamicResource MaterialDesignFloatingActionButton}"
37+
Margin="16 0 0 0"
38+
wpf:ShadowAssist.ShadowDepth="Depth2"
39+
>2</Button>
40+
<Button Style="{DynamicResource MaterialDesignFloatingActionButton}"
41+
Margin="16 0 0 0"
42+
>3</Button>
43+
<Button Style="{DynamicResource MaterialDesignFloatingActionButton}"
44+
Margin="16 0 0 0"
45+
wpf:ShadowAssist.ShadowDepth="Depth4"
46+
>4</Button>
47+
<Button Style="{DynamicResource MaterialDesignFloatingActionButton}"
48+
Margin="16 0 0 0"
49+
wpf:ShadowAssist.ShadowDepth="Depth5"
50+
>5</Button>
51+
</StackPanel>
52+
<StackPanel Orientation="Horizontal" Margin="0 32 0 0">
53+
<wpf:Card wpf:ShadowAssist.ShadowDepth="Depth1" Padding="32">DEPTH 1</wpf:Card>
54+
<wpf:Card wpf:ShadowAssist.ShadowDepth="Depth2" Margin="16 0 0 0" Padding="32">DEPTH 2</wpf:Card>
55+
<wpf:Card wpf:ShadowAssist.ShadowDepth="Depth3" Margin="16 0 0 0" Padding="32">DEPTH 3</wpf:Card>
56+
<wpf:Card wpf:ShadowAssist.ShadowDepth="Depth4" Margin="16 0 0 0" Padding="32">DEPTH 4</wpf:Card>
57+
<wpf:Card wpf:ShadowAssist.ShadowDepth="Depth5" Margin="16 0 0 0" Padding="32">DEPTH 5</wpf:Card>
58+
</StackPanel>
59+
60+
</StackPanel>
61+
</UserControl>

MainDemo.Wpf/Shadows.xaml.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Windows;
7+
using System.Windows.Controls;
8+
using System.Windows.Data;
9+
using System.Windows.Documents;
10+
using System.Windows.Input;
11+
using System.Windows.Media;
12+
using System.Windows.Media.Imaging;
13+
using System.Windows.Navigation;
14+
using System.Windows.Shapes;
15+
16+
namespace MaterialDesignColors.WpfExample
17+
{
18+
/// <summary>
19+
/// Interaction logic for Shadows.xaml
20+
/// </summary>
21+
public partial class Shadows : UserControl
22+
{
23+
public Shadows()
24+
{
25+
InitializeComponent();
26+
}
27+
}
28+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Data;
9+
10+
namespace MaterialDesignThemes.Wpf.Converters
11+
{
12+
public class ShadowConverter : IValueConverter
13+
{
14+
private static readonly IDictionary<ShadowDepth, object> ShadowsDictionary;
15+
public static readonly ShadowConverter Instance = new ShadowConverter();
16+
17+
static ShadowConverter()
18+
{
19+
var resourceDictionary = new ResourceDictionary { Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml", UriKind.Absolute) };
20+
21+
ShadowsDictionary = new Dictionary<ShadowDepth, object>
22+
{
23+
{ ShadowDepth.Depth1, resourceDictionary["MaterialDesignShadowDepth1"] },
24+
{ ShadowDepth.Depth2, resourceDictionary["MaterialDesignShadowDepth2"] },
25+
{ ShadowDepth.Depth3, resourceDictionary["MaterialDesignShadowDepth3"] },
26+
{ ShadowDepth.Depth4, resourceDictionary["MaterialDesignShadowDepth4"] },
27+
{ ShadowDepth.Depth5, resourceDictionary["MaterialDesignShadowDepth5"] },
28+
};
29+
}
30+
31+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
32+
{
33+
return ShadowsDictionary[(ShadowDepth) value];
34+
}
35+
36+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
37+
{
38+
throw new NotImplementedException();
39+
}
40+
}
41+
}

MaterialDesignThemes.Wpf/MaterialDesignThemes.Wpf.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
<Compile Include="Converters\NotConverter.cs" />
214214
<Compile Include="Converters\NotNullToVisibilityConverter.cs" />
215215
<Compile Include="Converters\NotZeroToVisibilityConverter.cs" />
216+
<Compile Include="Converters\ShadowConverter.cs" />
216217
<Compile Include="Converters\SizeToRectConverter.cs" />
217218
<Compile Include="CustomPopupPlacementCallbackHelper.cs" />
218219
<Compile Include="CustomValidationPopup.cs" />
@@ -242,6 +243,7 @@
242243
</Compile>
243244
<Compile Include="RatingBar.cs" />
244245
<Compile Include="RatingBarButton.cs" />
246+
<Compile Include="ShadowAssist.cs" />
245247
<Compile Include="TextFieldAssist.cs" />
246248
<Compile Include="Converters\TextFieldHintVisibilityConverter.cs" />
247249
<Compile Include="TimePicker.cs" />
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Windows;
2+
3+
namespace MaterialDesignThemes.Wpf
4+
{
5+
public enum ShadowDepth
6+
{
7+
Depth1,
8+
Depth2,
9+
Depth3,
10+
Depth4,
11+
Depth5
12+
}
13+
14+
public static class ShadowAssist
15+
{
16+
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(
17+
"ShadowDepth", typeof (ShadowDepth), typeof (ShadowAssist), new PropertyMetadata(default(ShadowDepth)));
18+
19+
public static void SetShadowDepth(DependencyObject element, ShadowDepth value)
20+
{
21+
element.SetValue(ShadowDepthProperty, value);
22+
}
23+
24+
public static ShadowDepth GetShadowDepth(DependencyObject element)
25+
{
26+
return (ShadowDepth) element.GetValue(ShadowDepthProperty);
27+
}
28+
29+
}
30+
}

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Button.xaml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf"
4-
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options">
4+
xmlns:po="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
5+
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters">
56

67
<ResourceDictionary.MergedDictionaries>
78
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml" />
@@ -24,6 +25,7 @@
2425
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
2526
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
2627
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
28+
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth1" />
2729
<Setter Property="TextBlock.FontWeight" Value="DemiBold"/>
2830
<Setter Property="TextBlock.FontSize" Value="15"/>
2931
<Setter Property="BorderThickness" Value="1"/>
@@ -58,7 +60,8 @@
5860
</Storyboard>
5961
</ControlTemplate.Resources>
6062
<Grid>
61-
<Border Background="{TemplateBinding Background}" Effect="{StaticResource MaterialDesignShadowDepth1}" />
63+
<Border Background="{TemplateBinding Background}"
64+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}" />
6265
<Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="2" />
6366
<Border Background="White" CornerRadius="2" x:Name="ClickBorder" Opacity="0" RenderTransformOrigin="0.5,0.5" >
6467
<Border.RenderTransform>
@@ -222,14 +225,16 @@
222225
<Setter Property="BorderThickness" Value="1"/>
223226
<Setter Property="HorizontalContentAlignment" Value="Center"/>
224227
<Setter Property="VerticalContentAlignment" Value="Center"/>
228+
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth3" />
225229
<Setter Property="Padding" Value="1"/>
226230
<Setter Property="Width" Value="40" />
227231
<Setter Property="Height" Value="40" />
228232
<Setter Property="Template">
229233
<Setter.Value>
230234
<ControlTemplate TargetType="{x:Type Button}">
231235
<Grid>
232-
<Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}" Effect="{DynamicResource MaterialDesignShadowDepth1}"
236+
<Ellipse Fill="{TemplateBinding Background}" Stroke="{TemplateBinding BorderBrush}" StrokeThickness="{TemplateBinding BorderThickness}"
237+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
233238
x:Name="border" />
234239
<wpf:Ripple Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Focusable="False"
235240
Feedback="White"

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Card.xaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<ControlTemplate TargetType="{x:Type wpf:Card}" x:Key="CardTemplate">
1313
<Border Margin="{TemplateBinding Margin}"
14-
Effect="{StaticResource MaterialDesignShadowDepth2}"
14+
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}"
1515
CornerRadius="{TemplateBinding UniformCornerRadius}" Background="Transparent">
1616
<Border Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
1717
x:Name="PART_ClipBorder"
@@ -28,6 +28,7 @@
2828
<Setter Property="Template" Value="{StaticResource CardTemplate}" />
2929
<Setter Property="Background" Value="{DynamicResource MaterialDesignPaper}" />
3030
<Setter Property="VerticalAlignment" Value="Top" />
31+
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth2" />
3132
</Style>
3233

3334
</ResourceDictionary>

0 commit comments

Comments
 (0)