Add control dynamically using material design WPF
up vote
0
down vote
favorite
I am new in WPF. One of my desktop application I want to use Material Design. I added MaterialDesign nuget package and implemented successfully.
Here I have a situation where I need to add a button or card on my button click. those are fully dynamic.
Can I add material design controls dynamically from C#?
Help me with example.
Thanks in advance
wpf material-design-in-xaml
add a comment |
up vote
0
down vote
favorite
I am new in WPF. One of my desktop application I want to use Material Design. I added MaterialDesign nuget package and implemented successfully.
Here I have a situation where I need to add a button or card on my button click. those are fully dynamic.
Can I add material design controls dynamically from C#?
Help me with example.
Thanks in advance
wpf material-design-in-xaml
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new in WPF. One of my desktop application I want to use Material Design. I added MaterialDesign nuget package and implemented successfully.
Here I have a situation where I need to add a button or card on my button click. those are fully dynamic.
Can I add material design controls dynamically from C#?
Help me with example.
Thanks in advance
wpf material-design-in-xaml
I am new in WPF. One of my desktop application I want to use Material Design. I added MaterialDesign nuget package and implemented successfully.
Here I have a situation where I need to add a button or card on my button click. those are fully dynamic.
Can I add material design controls dynamically from C#?
Help me with example.
Thanks in advance
wpf material-design-in-xaml
wpf material-design-in-xaml
asked Nov 10 at 4:22
Nirav.Bhatt
43
43
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
I believe you might be using Material Design In XAML. Make sure to check out the demo project. It contains all the answers to common control usage.
OPTION ONE.
You would usually use the "ItemsControl" to load objects into view dynamically. The items control is linked to a items source in your back-end view model. The source should be of type ObservableCollection. When ever you feed items to this source, the ItemsControl will update and insert based on the template it's provided.
Below is a basic short example.
xaml
<ItemsControl ItemsSource="Binding ItemsToLoad" Grid.IsSharedSizeScope="True" Margin="12 0 12 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>Hi</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm assuming you have a good understanding of bindings, so I wont be posting all the back end code.
OPTION TWO
programmatically
Seeing as you mentioned that you want to do it programmatically, you could also just do this:
Step 1 : Create a grid and set a layout for you to work with.
<Grid x:Name="grid_Main">
<Grid.ColumnsDefinition>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnsDefinition>
<Grid.RowDefinition>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinition>
</Grid>
Step 2: Code behind.
private void AddControlToGrid()
Button button = new Button();
grid_Main.Children.Add(button );
Grid.SetRow(button , 1); // 1 is row index
Grid.SetColumn(button , 0);// 0 is column index
Just a Note of the material design usage. You need to reference the lib before you can use it in your view.
Like this :
Add this to your application resources.
<Application x:Class="demo_app.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:demo_app">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Add this ref to any view you need to use a material design control on.
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
You can then use material design controls like this.
<materialDesign:Card Width="200">
<Grid>
<!--Content-->
</Grid>
</materialDesign:Card>
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
add a comment |
up vote
0
down vote
accepted
I took reference with following link for a solution with material design.
How to generate WPF controls automatically based on XML file?
I don't know if this way is efficient for using in material design or not but I succeed in getting result for Card control.
StackPanel stackPanel = new StackPanel();
MaterialDesignThemes.Wpf.Card card = new MaterialDesignThemes.Wpf.Card();
StringBuilder sb = new StringBuilder();
//Create card
sb.Append(@"<materialDesign:Card 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' Background='#03a9f4' Foreground = 'DynamicResource PrimaryHueDarkForegroundBrush' Padding = '0' Width = '200'> ");
sb.Append(@"<Grid><Grid.RowDefinitions><RowDefinition Height='Auto' /><RowDefinition Height = 'Auto' /><RowDefinition Height = 'Auto' /></Grid.RowDefinitions> ");
sb.Append(@"<TextBlock Grid.Row='0' Margin = '16 16 16 4' Style = 'StaticResource MaterialDesignHeadlineTextBlock'> Call Jennifer </TextBlock><Separator Grid.Row = '1' Style = 'StaticResource MaterialDesignLightSeparator' /><TextBlock Grid.Row = '2' Margin = '16 0 16 8' VerticalAlignment = 'Center' HorizontalAlignment = 'Left' Style = 'StaticResource MaterialDesignBody2TextBlock'> March 19, 2016 </TextBlock>");
sb.Append(@"</Grid></materialDesign:Card>");
card = (MaterialDesignThemes.Wpf.Card)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(card);
this.Content = stackPanel;
If we look closely,
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'
above properties are impotent to put when preparing new dynamic control.
Result I got from above code Dynamic Material design control
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I believe you might be using Material Design In XAML. Make sure to check out the demo project. It contains all the answers to common control usage.
OPTION ONE.
You would usually use the "ItemsControl" to load objects into view dynamically. The items control is linked to a items source in your back-end view model. The source should be of type ObservableCollection. When ever you feed items to this source, the ItemsControl will update and insert based on the template it's provided.
Below is a basic short example.
xaml
<ItemsControl ItemsSource="Binding ItemsToLoad" Grid.IsSharedSizeScope="True" Margin="12 0 12 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>Hi</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm assuming you have a good understanding of bindings, so I wont be posting all the back end code.
OPTION TWO
programmatically
Seeing as you mentioned that you want to do it programmatically, you could also just do this:
Step 1 : Create a grid and set a layout for you to work with.
<Grid x:Name="grid_Main">
<Grid.ColumnsDefinition>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnsDefinition>
<Grid.RowDefinition>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinition>
</Grid>
Step 2: Code behind.
private void AddControlToGrid()
Button button = new Button();
grid_Main.Children.Add(button );
Grid.SetRow(button , 1); // 1 is row index
Grid.SetColumn(button , 0);// 0 is column index
Just a Note of the material design usage. You need to reference the lib before you can use it in your view.
Like this :
Add this to your application resources.
<Application x:Class="demo_app.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:demo_app">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Add this ref to any view you need to use a material design control on.
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
You can then use material design controls like this.
<materialDesign:Card Width="200">
<Grid>
<!--Content-->
</Grid>
</materialDesign:Card>
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
add a comment |
up vote
0
down vote
I believe you might be using Material Design In XAML. Make sure to check out the demo project. It contains all the answers to common control usage.
OPTION ONE.
You would usually use the "ItemsControl" to load objects into view dynamically. The items control is linked to a items source in your back-end view model. The source should be of type ObservableCollection. When ever you feed items to this source, the ItemsControl will update and insert based on the template it's provided.
Below is a basic short example.
xaml
<ItemsControl ItemsSource="Binding ItemsToLoad" Grid.IsSharedSizeScope="True" Margin="12 0 12 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>Hi</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm assuming you have a good understanding of bindings, so I wont be posting all the back end code.
OPTION TWO
programmatically
Seeing as you mentioned that you want to do it programmatically, you could also just do this:
Step 1 : Create a grid and set a layout for you to work with.
<Grid x:Name="grid_Main">
<Grid.ColumnsDefinition>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnsDefinition>
<Grid.RowDefinition>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinition>
</Grid>
Step 2: Code behind.
private void AddControlToGrid()
Button button = new Button();
grid_Main.Children.Add(button );
Grid.SetRow(button , 1); // 1 is row index
Grid.SetColumn(button , 0);// 0 is column index
Just a Note of the material design usage. You need to reference the lib before you can use it in your view.
Like this :
Add this to your application resources.
<Application x:Class="demo_app.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:demo_app">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Add this ref to any view you need to use a material design control on.
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
You can then use material design controls like this.
<materialDesign:Card Width="200">
<Grid>
<!--Content-->
</Grid>
</materialDesign:Card>
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
add a comment |
up vote
0
down vote
up vote
0
down vote
I believe you might be using Material Design In XAML. Make sure to check out the demo project. It contains all the answers to common control usage.
OPTION ONE.
You would usually use the "ItemsControl" to load objects into view dynamically. The items control is linked to a items source in your back-end view model. The source should be of type ObservableCollection. When ever you feed items to this source, the ItemsControl will update and insert based on the template it's provided.
Below is a basic short example.
xaml
<ItemsControl ItemsSource="Binding ItemsToLoad" Grid.IsSharedSizeScope="True" Margin="12 0 12 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>Hi</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm assuming you have a good understanding of bindings, so I wont be posting all the back end code.
OPTION TWO
programmatically
Seeing as you mentioned that you want to do it programmatically, you could also just do this:
Step 1 : Create a grid and set a layout for you to work with.
<Grid x:Name="grid_Main">
<Grid.ColumnsDefinition>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnsDefinition>
<Grid.RowDefinition>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinition>
</Grid>
Step 2: Code behind.
private void AddControlToGrid()
Button button = new Button();
grid_Main.Children.Add(button );
Grid.SetRow(button , 1); // 1 is row index
Grid.SetColumn(button , 0);// 0 is column index
Just a Note of the material design usage. You need to reference the lib before you can use it in your view.
Like this :
Add this to your application resources.
<Application x:Class="demo_app.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:demo_app">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Add this ref to any view you need to use a material design control on.
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
You can then use material design controls like this.
<materialDesign:Card Width="200">
<Grid>
<!--Content-->
</Grid>
</materialDesign:Card>
I believe you might be using Material Design In XAML. Make sure to check out the demo project. It contains all the answers to common control usage.
OPTION ONE.
You would usually use the "ItemsControl" to load objects into view dynamically. The items control is linked to a items source in your back-end view model. The source should be of type ObservableCollection. When ever you feed items to this source, the ItemsControl will update and insert based on the template it's provided.
Below is a basic short example.
xaml
<ItemsControl ItemsSource="Binding ItemsToLoad" Grid.IsSharedSizeScope="True" Margin="12 0 12 0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>Hi</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
I'm assuming you have a good understanding of bindings, so I wont be posting all the back end code.
OPTION TWO
programmatically
Seeing as you mentioned that you want to do it programmatically, you could also just do this:
Step 1 : Create a grid and set a layout for you to work with.
<Grid x:Name="grid_Main">
<Grid.ColumnsDefinition>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnsDefinition>
<Grid.RowDefinition>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinition>
</Grid>
Step 2: Code behind.
private void AddControlToGrid()
Button button = new Button();
grid_Main.Children.Add(button );
Grid.SetRow(button , 1); // 1 is row index
Grid.SetColumn(button , 0);// 0 is column index
Just a Note of the material design usage. You need to reference the lib before you can use it in your view.
Like this :
Add this to your application resources.
<Application x:Class="demo_app.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:demo_app">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.DeepPurple.xaml" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Lime.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Add this ref to any view you need to use a material design control on.
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
You can then use material design controls like this.
<materialDesign:Card Width="200">
<Grid>
<!--Content-->
</Grid>
</materialDesign:Card>
edited Nov 10 at 11:23
answered Nov 10 at 10:49
B.Spangenberg
1079
1079
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
add a comment |
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
Thanks for the reply. I am sorry if I didn't made clear point in my question. I followed this link and got answer stackoverflow.com/questions/4990624/…
– Nirav.Bhatt
Nov 10 at 14:48
add a comment |
up vote
0
down vote
accepted
I took reference with following link for a solution with material design.
How to generate WPF controls automatically based on XML file?
I don't know if this way is efficient for using in material design or not but I succeed in getting result for Card control.
StackPanel stackPanel = new StackPanel();
MaterialDesignThemes.Wpf.Card card = new MaterialDesignThemes.Wpf.Card();
StringBuilder sb = new StringBuilder();
//Create card
sb.Append(@"<materialDesign:Card 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' Background='#03a9f4' Foreground = 'DynamicResource PrimaryHueDarkForegroundBrush' Padding = '0' Width = '200'> ");
sb.Append(@"<Grid><Grid.RowDefinitions><RowDefinition Height='Auto' /><RowDefinition Height = 'Auto' /><RowDefinition Height = 'Auto' /></Grid.RowDefinitions> ");
sb.Append(@"<TextBlock Grid.Row='0' Margin = '16 16 16 4' Style = 'StaticResource MaterialDesignHeadlineTextBlock'> Call Jennifer </TextBlock><Separator Grid.Row = '1' Style = 'StaticResource MaterialDesignLightSeparator' /><TextBlock Grid.Row = '2' Margin = '16 0 16 8' VerticalAlignment = 'Center' HorizontalAlignment = 'Left' Style = 'StaticResource MaterialDesignBody2TextBlock'> March 19, 2016 </TextBlock>");
sb.Append(@"</Grid></materialDesign:Card>");
card = (MaterialDesignThemes.Wpf.Card)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(card);
this.Content = stackPanel;
If we look closely,
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'
above properties are impotent to put when preparing new dynamic control.
Result I got from above code Dynamic Material design control
add a comment |
up vote
0
down vote
accepted
I took reference with following link for a solution with material design.
How to generate WPF controls automatically based on XML file?
I don't know if this way is efficient for using in material design or not but I succeed in getting result for Card control.
StackPanel stackPanel = new StackPanel();
MaterialDesignThemes.Wpf.Card card = new MaterialDesignThemes.Wpf.Card();
StringBuilder sb = new StringBuilder();
//Create card
sb.Append(@"<materialDesign:Card 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' Background='#03a9f4' Foreground = 'DynamicResource PrimaryHueDarkForegroundBrush' Padding = '0' Width = '200'> ");
sb.Append(@"<Grid><Grid.RowDefinitions><RowDefinition Height='Auto' /><RowDefinition Height = 'Auto' /><RowDefinition Height = 'Auto' /></Grid.RowDefinitions> ");
sb.Append(@"<TextBlock Grid.Row='0' Margin = '16 16 16 4' Style = 'StaticResource MaterialDesignHeadlineTextBlock'> Call Jennifer </TextBlock><Separator Grid.Row = '1' Style = 'StaticResource MaterialDesignLightSeparator' /><TextBlock Grid.Row = '2' Margin = '16 0 16 8' VerticalAlignment = 'Center' HorizontalAlignment = 'Left' Style = 'StaticResource MaterialDesignBody2TextBlock'> March 19, 2016 </TextBlock>");
sb.Append(@"</Grid></materialDesign:Card>");
card = (MaterialDesignThemes.Wpf.Card)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(card);
this.Content = stackPanel;
If we look closely,
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'
above properties are impotent to put when preparing new dynamic control.
Result I got from above code Dynamic Material design control
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I took reference with following link for a solution with material design.
How to generate WPF controls automatically based on XML file?
I don't know if this way is efficient for using in material design or not but I succeed in getting result for Card control.
StackPanel stackPanel = new StackPanel();
MaterialDesignThemes.Wpf.Card card = new MaterialDesignThemes.Wpf.Card();
StringBuilder sb = new StringBuilder();
//Create card
sb.Append(@"<materialDesign:Card 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' Background='#03a9f4' Foreground = 'DynamicResource PrimaryHueDarkForegroundBrush' Padding = '0' Width = '200'> ");
sb.Append(@"<Grid><Grid.RowDefinitions><RowDefinition Height='Auto' /><RowDefinition Height = 'Auto' /><RowDefinition Height = 'Auto' /></Grid.RowDefinitions> ");
sb.Append(@"<TextBlock Grid.Row='0' Margin = '16 16 16 4' Style = 'StaticResource MaterialDesignHeadlineTextBlock'> Call Jennifer </TextBlock><Separator Grid.Row = '1' Style = 'StaticResource MaterialDesignLightSeparator' /><TextBlock Grid.Row = '2' Margin = '16 0 16 8' VerticalAlignment = 'Center' HorizontalAlignment = 'Left' Style = 'StaticResource MaterialDesignBody2TextBlock'> March 19, 2016 </TextBlock>");
sb.Append(@"</Grid></materialDesign:Card>");
card = (MaterialDesignThemes.Wpf.Card)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(card);
this.Content = stackPanel;
If we look closely,
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'
above properties are impotent to put when preparing new dynamic control.
Result I got from above code Dynamic Material design control
I took reference with following link for a solution with material design.
How to generate WPF controls automatically based on XML file?
I don't know if this way is efficient for using in material design or not but I succeed in getting result for Card control.
StackPanel stackPanel = new StackPanel();
MaterialDesignThemes.Wpf.Card card = new MaterialDesignThemes.Wpf.Card();
StringBuilder sb = new StringBuilder();
//Create card
sb.Append(@"<materialDesign:Card 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' Background='#03a9f4' Foreground = 'DynamicResource PrimaryHueDarkForegroundBrush' Padding = '0' Width = '200'> ");
sb.Append(@"<Grid><Grid.RowDefinitions><RowDefinition Height='Auto' /><RowDefinition Height = 'Auto' /><RowDefinition Height = 'Auto' /></Grid.RowDefinitions> ");
sb.Append(@"<TextBlock Grid.Row='0' Margin = '16 16 16 4' Style = 'StaticResource MaterialDesignHeadlineTextBlock'> Call Jennifer </TextBlock><Separator Grid.Row = '1' Style = 'StaticResource MaterialDesignLightSeparator' /><TextBlock Grid.Row = '2' Margin = '16 0 16 8' VerticalAlignment = 'Center' HorizontalAlignment = 'Left' Style = 'StaticResource MaterialDesignBody2TextBlock'> March 19, 2016 </TextBlock>");
sb.Append(@"</Grid></materialDesign:Card>");
card = (MaterialDesignThemes.Wpf.Card)XamlReader.Parse(sb.ToString());
// Add created button to previously created container.
stackPanel.Children.Add(card);
this.Content = stackPanel;
If we look closely,
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'
above properties are impotent to put when preparing new dynamic control.
Result I got from above code Dynamic Material design control
answered Nov 10 at 14:45
Nirav.Bhatt
43
43
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235969%2fadd-control-dynamically-using-material-design-wpf%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown