Skip to content

Commit 7b3837b

Browse files
committed
dialogs entirely from code
1 parent 8e6eb97 commit 7b3837b

File tree

7 files changed

+321
-115
lines changed

7 files changed

+321
-115
lines changed

MainDemo.Wpf/Dialogs.xaml

Lines changed: 105 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,120 +15,116 @@
1515
</ResourceDictionary.MergedDictionaries>
1616
</ResourceDictionary>
1717
</UserControl.Resources>
18-
<Grid>
18+
19+
<Grid VerticalAlignment="Center">
1920
<Grid.RowDefinitions>
2021
<RowDefinition Height="Auto" />
21-
<RowDefinition Height="*" />
22+
<RowDefinition Height="*" />
2223
</Grid.RowDefinitions>
23-
<StackPanel>
24-
<TextBlock>WORK IN PROGRESS...DO NOT USE YET...API NOT FINALISED.</TextBlock>
25-
<!--
26-
<TextBlock>Dialogs can be hosted at any level of you visual tree.</TextBlock>
27-
<TextBlock>When a dialog is open the underlying content will be disabled.</TextBlock>
28-
-->
29-
</StackPanel>
30-
31-
<Grid Grid.Row="1">
32-
<Grid.RowDefinitions>
33-
<RowDefinition Height="Auto" />
34-
<RowDefinition Height="*" />
35-
</Grid.RowDefinitions>
36-
<Grid.ColumnDefinitions>
37-
<ColumnDefinition Width="1*" />
38-
<ColumnDefinition Width="1*" />
39-
<ColumnDefinition Width="1*" />
40-
<ColumnDefinition Width="1*" />
41-
</Grid.ColumnDefinitions>
24+
<Grid.ColumnDefinitions>
25+
<ColumnDefinition Width="1*" />
26+
<ColumnDefinition Width="1*" />
27+
<ColumnDefinition Width="1*" />
28+
</Grid.ColumnDefinitions>
4229

43-
<!--#region SAMPLE 1-->
44-
<TextBlock TextWrapping="Wrap" HorizontalAlignment="Center"
45-
Margin="16">SAMPLE 1: Localised dialog encapsulating specific content, launched from a routed command, response handled in code-behind.</TextBlock>
46-
<wpf:DialogHost DialogClosing="Sample1_DialogHost_OnDialogClosing"
47-
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
48-
<wpf:DialogHost.DialogContent>
49-
<StackPanel Margin="16">
50-
<TextBlock>Add a new fruit.</TextBlock>
51-
<TextBox Margin="0 8 0 0" HorizontalAlignment="Stretch" x:Name="FruitTextBox" />
52-
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
53-
<Button Style="{StaticResource MaterialDesignFlatButton}"
54-
IsDefault="True"
55-
Margin="0 8 8 0"
56-
Command="wpf:DialogHost.CloseDialogCommand">
57-
<Button.CommandParameter>
58-
<system:Boolean>True</system:Boolean>
59-
</Button.CommandParameter>
60-
ACCEPT
61-
</Button>
62-
<Button Style="{StaticResource MaterialDesignFlatButton}"
63-
IsCancel="True"
64-
Margin="0 8 8 0"
65-
Command="wpf:DialogHost.CloseDialogCommand">
66-
<Button.CommandParameter>
67-
<system:Boolean>False</system:Boolean>
68-
</Button.CommandParameter>
69-
CANCEL
70-
</Button>
71-
</StackPanel>
72-
</StackPanel>
73-
</wpf:DialogHost.DialogContent>
74-
<Border BorderThickness="1" BorderBrush="{DynamicResource PrimaryHueMidBrush}"
75-
MinWidth="256" MinHeight="256">
76-
<Grid>
77-
<Grid.RowDefinitions>
78-
<RowDefinition Height="*" />
79-
<RowDefinition Height="Auto" />
80-
</Grid.RowDefinitions>
81-
<ListBox x:Name="FruitListBox">
82-
<ListBoxItem>Apple</ListBoxItem>
83-
<ListBoxItem>Banana</ListBoxItem>
84-
<ListBoxItem>Pear</ListBoxItem>
85-
</ListBox>
86-
<wpf:ColorZone Mode="PrimaryMid" Grid.Row="1">
87-
<TextBlock Margin="16">Fruit Bowl</TextBlock>
88-
</wpf:ColorZone>
89-
<Button Style="{StaticResource MaterialDesignFloatingActionAccentButton}"
90-
Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
91-
VerticalAlignment="Bottom" HorizontalAlignment="Right"
92-
Grid.Row="0" Margin="0 0 28 -20">
93-
<Viewbox Width="22" Height="22">
94-
<Canvas Width="24" Height="24">
95-
<Path Data="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Foreground}" />
96-
</Canvas>
97-
</Viewbox>
30+
<!--#region SAMPLE 1-->
31+
<TextBlock TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Top"
32+
Margin="16">SAMPLE 1: Localised dialog encapsulating specific content, launched from a routed command, response handled in code-behind.</TextBlock>
33+
<wpf:DialogHost DialogClosing="Sample1_DialogHost_OnDialogClosing"
34+
Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
35+
<wpf:DialogHost.DialogContent>
36+
<StackPanel Margin="16">
37+
<TextBlock>Add a new fruit.</TextBlock>
38+
<TextBox Margin="0 8 0 0" HorizontalAlignment="Stretch" x:Name="FruitTextBox" />
39+
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
40+
<Button Style="{StaticResource MaterialDesignFlatButton}"
41+
IsDefault="True"
42+
Margin="0 8 8 0"
43+
Command="wpf:DialogHost.CloseDialogCommand">
44+
<Button.CommandParameter>
45+
<system:Boolean>True</system:Boolean>
46+
</Button.CommandParameter>
47+
ACCEPT
48+
</Button>
49+
<Button Style="{StaticResource MaterialDesignFlatButton}"
50+
IsCancel="True"
51+
Margin="0 8 8 0"
52+
Command="wpf:DialogHost.CloseDialogCommand">
53+
<Button.CommandParameter>
54+
<system:Boolean>False</system:Boolean>
55+
</Button.CommandParameter>
56+
CANCEL
9857
</Button>
99-
</Grid>
100-
</Border>
101-
</wpf:DialogHost>
102-
<!--#endregion -->
58+
</StackPanel>
59+
</StackPanel>
60+
</wpf:DialogHost.DialogContent>
61+
<Border BorderThickness="1" BorderBrush="{DynamicResource PrimaryHueMidBrush}"
62+
MinWidth="256" MinHeight="256">
63+
<Grid>
64+
<Grid.RowDefinitions>
65+
<RowDefinition Height="*" />
66+
<RowDefinition Height="Auto" />
67+
</Grid.RowDefinitions>
68+
<ListBox x:Name="FruitListBox">
69+
<ListBoxItem>Apple</ListBoxItem>
70+
<ListBoxItem>Banana</ListBoxItem>
71+
<ListBoxItem>Pear</ListBoxItem>
72+
</ListBox>
73+
<wpf:ColorZone Mode="PrimaryMid" Grid.Row="1">
74+
<TextBlock Margin="16">Fruit Bowl</TextBlock>
75+
</wpf:ColorZone>
76+
<Button Style="{StaticResource MaterialDesignFloatingActionAccentButton}"
77+
Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
78+
VerticalAlignment="Bottom" HorizontalAlignment="Right"
79+
Grid.Row="0" Margin="0 0 28 -20">
80+
<Viewbox Width="22" Height="22">
81+
<Canvas Width="24" Height="24">
82+
<Path Data="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Button}, Path=Foreground}" />
83+
</Canvas>
84+
</Viewbox>
85+
</Button>
86+
</Grid>
87+
</Border>
88+
</wpf:DialogHost>
89+
<!--#endregion -->
10390

104-
<!--#region SAMPLE 2-->
105-
<TextBlock Grid.Column="1" Grid.Row="0" TextWrapping="Wrap">SAMPLE 2: Using OpenDialog, passing content via the Parameter. You can pass a view model, provided a corresponding DataTemplate can be found in the scope of the root DialogHost.</TextBlock>
106-
<StackPanel Grid.Column="1" Grid.Row="1">
107-
<!--the request to open the dialog will bubble up to the top-most DialogHost, but we can used the attached property based event to handle the response -->
108-
<Button Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
109-
wpf:DialogHost.DialogClosing="Sample2_DialogHost_OnDialogClosing"
110-
Width="128">
111-
<Button.CommandParameter>
112-
<StackPanel Margin="16">
113-
<ProgressBar Style="{DynamicResource MaterialDesignCircularProgressBar}" HorizontalAlignment="Center" Margin="16" IsIndeterminate="True" Value="0" />
114-
<Button Style="{StaticResource MaterialDesignFlatButton}" IsCancel="True"
115-
Command="{x:Static wpf:DialogHost.CloseDialogCommand}"
116-
CommandParameter="Sample2Cancel"
117-
HorizontalAlignment="Center">CANCEL</Button>
118-
</StackPanel>
119-
</Button.CommandParameter>
120-
PASS VIEW
121-
</Button>
122-
<Button Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
123-
Width="128" Margin="0 32 0 0">
124-
<Button.CommandParameter>
125-
<!-- the simplest view model of all, a DateTime. the view can be found in the resources of MainWindow.xaml -->
126-
<system:DateTime>1966-JUL-30</system:DateTime>
127-
</Button.CommandParameter>
128-
PASS MODEL
129-
</Button>
130-
</StackPanel>
131-
<!--#endregion-->
132-
</Grid>
91+
<!--#region SAMPLE 2-->
92+
<TextBlock Grid.Column="1" Grid.Row="0" TextWrapping="Wrap" VerticalAlignment="Top">SAMPLE 2: Top level dialog, using OpenDialog, passing content via the Parameter. You can pass a view model, provided a corresponding DataTemplate can be found in the scope of the root DialogHost.</TextBlock>
93+
<StackPanel Grid.Column="1" Grid.Row="1" VerticalAlignment="Center">
94+
<!--the request to open the dialog will bubble up to the top-most DialogHost, but we can used the attached property based event to handle the response -->
95+
<Button Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
96+
wpf:DialogHost.DialogClosing="Sample2_DialogHost_OnDialogClosing"
97+
Width="128">
98+
<Button.CommandParameter>
99+
<StackPanel Margin="16">
100+
<ProgressBar Style="{DynamicResource MaterialDesignCircularProgressBar}" HorizontalAlignment="Center" Margin="16" IsIndeterminate="True" Value="0" />
101+
<Button Style="{StaticResource MaterialDesignFlatButton}" IsCancel="True"
102+
Command="{x:Static wpf:DialogHost.CloseDialogCommand}"
103+
CommandParameter="Sample2Cancel"
104+
HorizontalAlignment="Center">CANCEL</Button>
105+
</StackPanel>
106+
</Button.CommandParameter>
107+
PASS VIEW
108+
</Button>
109+
<Button Command="{x:Static wpf:DialogHost.OpenDialogCommand}"
110+
Width="128" Margin="0 32 0 0">
111+
<Button.CommandParameter>
112+
<!-- the simplest view model of all, a DateTime. the view can be found in the resources of MainWindow.xaml -->
113+
<system:DateTime>1966-JUL-30</system:DateTime>
114+
</Button.CommandParameter>
115+
PASS MODEL
116+
</Button>
117+
</StackPanel>
118+
<!--#endregion-->
119+
120+
<!--#region SAMPLE 3-->
121+
<TextBlock Grid.Column="2" Grid.Row="0" TextWrapping="Wrap" VerticalAlignment="Top">SAMPLE 3: Open and listen to the dialog entirely from code in a view model.</TextBlock>
122+
<!-- Executes a command on DialogsViewModel.cs, which will launch the dialog from code -->
123+
<Button Grid.Column="2" Grid.Row="1" Command="{Binding RunDialogCommand}"
124+
Width="128">
125+
RUN CODE
126+
</Button>
127+
<!--#endregion-->
133128
</Grid>
129+
134130
</UserControl>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.Controls;
7+
using System.Windows.Input;
8+
using MaterialDesignThemes.Wpf;
9+
10+
namespace MaterialDesignColors.WpfExample.Domain
11+
{
12+
public class DialogsViewModel
13+
{
14+
public ICommand RunDialogCommand => new AnotherCommandImplementation(ExecuteRunDialog);
15+
16+
private async void ExecuteRunDialog(object o)
17+
{
18+
//let's set up a little MVVM, cos that's what the cool kids are doing:
19+
var view = new SampleDialog
20+
{
21+
DataContext = new object()
22+
};
23+
24+
//show the dialog
25+
var result = await DialogHost.Show(view, "RootDialog", ClosingEventHandler);
26+
27+
//check the result...
28+
Console.WriteLine("Dialog was closed, the CommandParameter used to close it was: " + (result ?? "NULL"));
29+
}
30+
31+
private void ClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
32+
{
33+
Console.WriteLine("You can intercept the closing event, and cancel here.");
34+
}
35+
}
36+
}

MainDemo.Wpf/Domain/SampleDialog.xaml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<UserControl x:Class="MaterialDesignColors.WpfExample.Domain.SampleDialog"
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.Domain"
7+
xmlns:wpf="clr-namespace:MaterialDesignThemes.Wpf;assembly=MaterialDesignThemes.Wpf"
8+
xmlns:system="clr-namespace:System;assembly=mscorlib"
9+
mc:Ignorable="d"
10+
d:DesignHeight="300" d:DesignWidth="300">
11+
<Grid Margin="16">
12+
13+
<Grid.RowDefinitions>
14+
<RowDefinition />
15+
<RowDefinition />
16+
<RowDefinition />
17+
<RowDefinition />
18+
</Grid.RowDefinitions>
19+
<TextBlock>New contact:</TextBlock>
20+
<TextBox wpf:TextFieldAssist.Hint="Name" Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
21+
Margin="0 6 0 0"
22+
FontSize="18" Grid.Row="1"
23+
/>
24+
<TextBox wpf:TextFieldAssist.Hint="Number" Style="{DynamicResource MaterialDesignFloatingHintTextBox}"
25+
Margin="0 8 0 0"
26+
FontSize="16" Grid.Row="2"
27+
/>
28+
<StackPanel Orientation="Horizontal" Grid.Row="3"
29+
Margin="0 16 0 0">
30+
<Button IsDefault="True" Style="{DynamicResource MaterialDesignFlatButton}"
31+
Command="{x:Static wpf:DialogHost.CloseDialogCommand}">
32+
<Button.CommandParameter>
33+
<system:Boolean>True</system:Boolean>
34+
</Button.CommandParameter>
35+
ACCEPT
36+
</Button>
37+
<Button IsCancel="True" Margin="8 0 0 0" Style="{DynamicResource MaterialDesignFlatButton}"
38+
Command="{x:Static wpf:DialogHost.CloseDialogCommand}">
39+
<Button.CommandParameter>
40+
<system:Boolean>False</system:Boolean>
41+
</Button.CommandParameter>
42+
CANCEL
43+
</Button>
44+
</StackPanel>
45+
46+
47+
</Grid>
48+
</UserControl>
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.Domain
17+
{
18+
/// <summary>
19+
/// Interaction logic for SampleDialog.xaml
20+
/// </summary>
21+
public partial class SampleDialog : UserControl
22+
{
23+
public SampleDialog()
24+
{
25+
InitializeComponent();
26+
}
27+
}
28+
}

MainDemo.Wpf/MainDemo.Wpf.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@
8383
</Compile>
8484
<Compile Include="Domain\AnotherCommandImplementation.cs" />
8585
<Compile Include="Domain\DemoItem.cs" />
86+
<Compile Include="Domain\DialogsViewModel.cs" />
8687
<Compile Include="Domain\ListFieldsViewModel.cs" />
88+
<Compile Include="Domain\SampleDialog.xaml.cs">
89+
<DependentUpon>SampleDialog.xaml</DependentUpon>
90+
</Compile>
8791
<Compile Include="Domain\SelectableViewModel.cs" />
8892
<Compile Include="Domain\TextFieldsViewModel.cs" />
8993
<Compile Include="Grids.xaml.cs">
@@ -134,6 +138,10 @@
134138
<SubType>Designer</SubType>
135139
<Generator>MSBuild:Compile</Generator>
136140
</Page>
141+
<Page Include="Domain\SampleDialog.xaml">
142+
<SubType>Designer</SubType>
143+
<Generator>MSBuild:Compile</Generator>
144+
</Page>
137145
<Page Include="Grids.xaml">
138146
<SubType>Designer</SubType>
139147
<Generator>MSBuild:Compile</Generator>

MainDemo.Wpf/MainWindow.xaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</ResourceDictionary>
3434
</Window.Resources>
3535

36-
<wpf:DialogHost>
36+
<wpf:DialogHost Identifier="RootDialog">
3737
<DockPanel>
3838
<wpf:ColorZone Padding="16" Effect="{StaticResource MaterialDesignShadowDepth2}"
3939
Mode="PrimaryMid" DockPanel.Dock="Top">
@@ -119,7 +119,11 @@
119119
</domain:DemoItem>
120120
<domain:DemoItem Name="Dialogs">
121121
<domain:DemoItem.Content>
122-
<wpfExample:Dialogs />
122+
<wpfExample:Dialogs>
123+
<wpfExample:Dialogs.DataContext>
124+
<domain:DialogsViewModel />
125+
</wpfExample:Dialogs.DataContext>
126+
</wpfExample:Dialogs>
123127
</domain:DemoItem.Content>
124128
</domain:DemoItem>
125129
<domain:DemoItem Name="Shadows">

0 commit comments

Comments
 (0)