Skip to content

Commit 017eebe

Browse files
committed
- Less generic name for button styles
- Allow the user to override the built-in button styles
1 parent 3229b33 commit 017eebe

File tree

7 files changed

+58
-47
lines changed

7 files changed

+58
-47
lines changed

MaterialDesign.DialogPlus.Examples/App.xaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,15 @@
88
<ResourceDictionary.MergedDictionaries>
99
<mdix:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
1010
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesign3.Defaults.xaml" />
11+
12+
<ResourceDictionary Source="pack://application:,,,/MaterialDesign.DialogPlus;component/Themes/Defaults.xaml" />
1113
</ResourceDictionary.MergedDictionaries>
14+
15+
<Style x:Key="MaterialDesign.DialogPlus.NegativeButton" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
16+
<Setter Property="Background" Value="Salmon"/>
17+
<Setter Property="BorderBrush" Value="Salmon"/>
18+
</Style>
19+
1220
</ResourceDictionary>
1321
</Application.Resources>
1422
</Application>

MaterialDesign.DialogPlus.Examples/FormInputDialog.xaml

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
UseLayoutRounding="True">
55

6-
<UserControl.Resources>
7-
<ResourceDictionary>
8-
<ResourceDictionary.MergedDictionaries>
9-
<ResourceDictionary Source="pack://application:,,,/MaterialDesign.DialogPlus;component/ButtonStyles.xaml"/>
10-
</ResourceDictionary.MergedDictionaries>
11-
</ResourceDictionary>
12-
</UserControl.Resources>
13-
146
<StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" Margin="20">
157
<TextBlock Style="{StaticResource MaterialDesignHeadline4TextBlock}" TextWrapping="Wrap"
168
Text="Info needed" Margin="0,0,0,10"/>
@@ -34,14 +26,13 @@
3426

3527
<TextBlock Grid.Row="2" Style="{StaticResource MaterialDesignTextBlock}" Text="Email" VerticalAlignment="Center"/>
3628
<TextBox Grid.Row="2" Grid.Column="1" Margin="10,0,0,0" Text="{Binding Email}" VerticalAlignment="Center"/>
37-
3829
</Grid>
3930

4031
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,20,0,0">
41-
<Button Content="Cancel" Style="{StaticResource DialogButtonStyle}" IsCancel="True"
32+
<Button Content="Cancel" Style="{StaticResource MaterialDesign.DialogPlus.NeutralButton}" IsCancel="True"
4233
Command="{Binding CancelCommand}"/>
43-
<Button Content="OK" Style="{StaticResource PositiveButtonStyle}" IsDefault="True"
44-
Command="{Binding SubmitCommand}"/>
34+
<Button Content="OK" Style="{StaticResource MaterialDesign.DialogPlus.PositiveButton}"
35+
Margin="10,0,0,0" IsDefault="True" Command="{Binding SubmitCommand}"/>
4536
</StackPanel>
4637
</StackPanel>
4738
</UserControl>

MaterialDesign.DialogPlus/ButtonStyles.xaml

Lines changed: 0 additions & 17 deletions
This file was deleted.

MaterialDesign.DialogPlus/Dialogs/Dialog.xaml

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
UseLayoutRounding="True">
55

6-
<UserControl.Resources>
7-
<ResourceDictionary>
8-
<ResourceDictionary.MergedDictionaries>
9-
<ResourceDictionary Source="pack://application:,,,/MaterialDesign.DialogPlus;component/ButtonStyles.xaml"/>
10-
</ResourceDictionary.MergedDictionaries>
11-
</ResourceDictionary>
12-
</UserControl.Resources>
13-
146
<StackPanel DataContext="{Binding RelativeSource={RelativeSource AncestorType=UserControl}}" Margin="20">
157
<TextBlock Style="{StaticResource MaterialDesignHeadline4TextBlock}" TextWrapping="Wrap" Text="{Binding Title}" Margin="0,0,0,10"/>
168
<TextBlock Style="{StaticResource MaterialDesignTextBlock}" TextWrapping="Wrap" Text="{Binding Message}"/>
@@ -23,16 +15,16 @@
2315
</ItemsControl.ItemsPanel>
2416
<ItemsControl.ItemTemplate>
2517
<DataTemplate>
26-
<Button x:Name="innerButton" Click="Button_Click" Content="{Binding Text}"/>
18+
<Button x:Name="innerButton" Click="Button_Click" Content="{Binding Text}" Margin="10,0,0,0"/>
2719
<DataTemplate.Triggers>
2820
<DataTrigger Binding="{Binding Connotation}" Value="Positive">
29-
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource PositiveButtonStyle}"/>
21+
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource MaterialDesign.DialogPlus.PositiveButton}"/>
3022
</DataTrigger>
3123
<DataTrigger Binding="{Binding Connotation}" Value="Negative">
32-
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource NegativeButtonStyle}"/>
24+
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource MaterialDesign.DialogPlus.NegativeButton}"/>
3325
</DataTrigger>
3426
<DataTrigger Binding="{Binding Connotation}" Value="Neutral">
35-
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource DialogButtonStyle}"/>
27+
<Setter TargetName="innerButton" Property="Style" Value="{StaticResource MaterialDesign.DialogPlus.NeutralButton}"/>
3628
</DataTrigger>
3729
<DataTrigger Binding="{Binding IsDefault}" Value="True">
3830
<Setter TargetName="innerButton" Property="IsDefault" Value="True"/>

MaterialDesign.DialogPlus/MaterialDesign.DialogPlus.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFrameworks>net462;net6.0-windows;net7.0-windows</TargetFrameworks>
55
<UseWPF>true</UseWPF>
66
<Title>DialogPlus for MaterialDesignInXaml</Title>
7-
<Version>1.0.0</Version>
7+
<Version>1.1.0</Version>
88
<Authors>Joshua Foster</Authors>
99
<Company>Josh2112 Apps</Company>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@@ -15,6 +15,7 @@
1515
<Description>A small package to make working with MaterialDesignInXaml dialogs a bit easier.</Description>
1616
<PackageTags>materialdesigninxaml;materialdesignthemes;materialdesigncolors;dialoghost;dialog</PackageTags>
1717
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
18+
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1819
</PropertyGroup>
1920

2021
<ItemGroup>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
3+
4+
<Style x:Key="MaterialDesign.DialogPlus.NeutralButton" TargetType="Button" BasedOn="{StaticResource MaterialDesignOutlinedButton}"/>
5+
6+
<Style x:Key="MaterialDesign.DialogPlus.PositiveButton" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}"/>
7+
8+
<Style x:Key="MaterialDesign.DialogPlus.NegativeButton" TargetType="Button" BasedOn="{StaticResource MaterialDesignOutlinedButton}">
9+
<Setter Property="BorderBrush" Value="{DynamicResource MaterialDesignValidationErrorBrush}"/>
10+
<Setter Property="Foreground" Value="{DynamicResource MaterialDesignValidationErrorBrush}"/>
11+
</Style>
12+
</ResourceDictionary>

README.md

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ MaterialDesignInXaml has great support for dialogs, but it's not clear how to us
1111
- `Dialog`: a generic dialog imlpementation with title, message and configurable buttons which should suit 99% of your dialog use-cases.
1212
- `ButtonDef`: A simple model for dialog buttons. Includes a label, a "connotation" (Positive, Negative, or Neutral) which determines how the button is drawn, and a couple properties to make the button activate on [Enter] or [Esc].
1313

14-
### Basic dialog
14+
## Basic dialog
1515

1616
Here's the code for the above screenshot:
1717
```
@@ -22,12 +22,36 @@ ButtonDef result = await this.ShowDialogForResultAsync( new Dialogs.Dialog( "Que
2222
ButtonDef.Positive( "Save it", isDefault: true ) ) );
2323
```
2424

25-
Our example dialog in this case implements `IHasDialog<ButtonDef>`, meaning the result will be one of the three buttons you passed in. But your dialog can return anything you like.
25+
It uses the built-in, generic `Dialog` class to show a title, message and three buttons. The `Dialog` class implements `IHasDialog<ButtonDef>`, meaning the result will be one of the buttons you passed in. If you write your own dialog, you can return anything you like.
2626

27-
### Custom dialog
27+
The built-in dialog requires a couple custom styles. If you want to use it, make sure you put the following in your `App.xaml` to import them:
28+
```
29+
<ResourceDictionary Source="pack://application:,,,/MaterialDesign.DialogPlus;component/Themes/Defaults.xaml" />
30+
```
31+
32+
### Basic dialog: Button connotations and restyling buttons
33+
34+
When defining buttons for the built-in dialog, you have 3 `Connotations`:
35+
- `Connotations.Neutral`: Used for an action with no side effects, like "Cancel" or "Continue". The style for this is `MaterialDesign.DialogPlus.NeutralButton` which is an outlined button with the primary theme color.
36+
- `Connotations.Positive`: Used for positive actions, such as acknowledging a message or saving a file. The style for this is `MaterialDesign.DialogPlus.PositiveButton` which is a raised button with the primary theme color.
37+
- `Connotations.Negative`: Used for destructive actions, such as discarding changes or deleting a message. The style for this is `MaterialDesign.DialogPlus.NegativeButton` which is an outlined button with the outline and text color set to `MaterialDesignValidationErrorBrush` which is reddish in color.
38+
39+
You can change how any of these buttons are rendered in the built-in `Dialog` by defining your own style with the same name. Just put this in your `App.xaml` after you import the default button styles. For example:
40+
```
41+
<Style x:Key="MaterialDesign.DialogPlus.NegativeButton" TargetType="Button" BasedOn="{StaticResource MaterialDesignRaisedButton}">
42+
<Setter Property="Background" Value="Salmon"/>
43+
<Setter Property="BorderBrush" Value="Salmon"/>
44+
</Style>
45+
```
46+
47+
Now buttons created with `ButtonDef.Negative()` will have a salmon-colored border and background.
48+
49+
## Custom dialog
50+
51+
The built-in `Dialog` class should suit 99% of use cases. But you can build a completely custom dialog if you like.
2852

29-
1. Create a `UserControl`. Design the dialog XAML however you like.
30-
2. Make your dialog implement `IHasDialogResult<T>` with the desired result type. For example, if your dialog is a text input, you might implement `IHasDialogResult<string>`.
53+
1. Create a `UserControl`. Write the dialog XAML and the code-behind however you like.
54+
2. Make the `UserControl` implement `IHasDialogResult<T>` with the desired result type. For example, if your dialog is a text input, you might implement `IHasDialogResult<string>`.
3155
3. To close your dialog, call `Result.Set` with the dialog result (for the text input example, this might be the user-input text, or `null` if the user canceled the dialog).
3256

3357
Check out the [main file](MaterialDesign.DialogPlus.Examples/MainWindow.xaml.cs) of the example project for the following examples:

0 commit comments

Comments
 (0)