How do I open/create a menu in WPf with Material Design - wpf

So I´m using Material Design and Caliburn micro.
I want to get used to Material Design and im new to WPF. But i have to use both because of work.
I want to have a Colorzone in the top of my form.
<materialDesign:ColorZone
Mode="PrimaryLight"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="4"
Padding="16">
<StackPanel
Orientation="Horizontal">
<ToggleButton
Style="{DynamicResource MaterialDesignHamburgerToggleButton}">
</ToggleButton>
<TextBlock
VerticalAlignment="Center"
Margin="16 0 0 0">
Material Design In XAML Toolkit
</TextBlock>
</StackPanel>
</materialDesign:ColorZone>
I got the hamburger button on the right and when i start it the Animation works but how do i open a drawer or a menu when the button is clicked and how do i close it when it is pressed again.
Where do i write the code what the button does?

So I found an Answer:
<materialDesign:ColorZone
Mode="PrimaryLight"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="4"
Padding="16">
<StackPanel
Orientation="Horizontal">
<ToggleButton
Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
CommandParameter="{x:Static Dock.Left}"
Style="{DynamicResource MaterialDesignHamburgerToggleButton}">
</ToggleButton>
<TextBlock
VerticalAlignment="Center"
Margin="16 0 0 0">
Material Design In XAML Toolkit
</TextBlock>
</StackPanel>
</materialDesign:ColorZone>
You have to Add the Command and a CommandParameter for the Hamburger Button.
Now we just need a Drawer that we can open:
<materialDesign:DrawerHost.LeftDrawerContent>
<StackPanel Margin="16" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" >
<Button
Margin="25,10,25,10"
VerticalAlignment="Top"
cal:Message.Attach="DoCoolStuff()"
Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
CommandParameter="{x:Static Dock.Left}">
BlaBlaBlaYour Button Text here
</Button>
<Button
</StackPanel>
</materialDesign:DrawerHost.LeftDrawerContent>
So now we got a Menubar and A Drawer but they wont work until we write the them both into the following:
<materialDesign:DrawerHost Grid.RowSpan="3" Grid.ColumnSpan="4" Margin="0,0,0,0" BorderThickness="2" BorderBrush="{DynamicResource MaterialDesignDivider}"
Panel.ZIndex="1">
<materialDesign:ColorZone...>
<materialDesign:DrawerHost.LeftDrawerContent...>
</materialDesign:DrawerHost>
It is important that we give the DrawerHost Panel.ZIndex = 1 because when you have a button where the Drawer should open the button will be in the Foreground but now the Drawer will be.
And thats it, now we have a menubar with a Hamburgerbutton that opens a drawer.
A tip for the Button i Added it does not need the Command and the commandparameter but it help when you want to close the drawer when a button is pressed.
I hope i could help somebody

Related

WPF how to set stackpanel as resources and reuse it in TabControls

I am new to C# and WPF so please give me some ideas:
I have an WPF app used to display some stack panels,all stack panels default Visibility is set to collapsed and they will switch to visible according to the received data.
Now I want to make all these stack panels to resources so I can reuse it in some new added tab controls and stack panels.
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
</Button.Content>
</Button>
</StackPanel>
Above is one example of stack panels I am using. In the code behind the function "Color_Click" will change this "ColorOption" stack panel state and do something.
However after I try to put this stack panel into Windows.Resources
<Window.Resources>
<StackPanel x:Name="ColorOption" Visibility="Collapsed" x:Key="ColorOption">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}" />
</Button.Content>
</Button>
</StackPanel>
</Window.Resources> (I also put the style files inside)
In the tab controls I did
<TabControl>
<TabItem Header="Tab 1" Content="{StaticResource ColorOption}"/>
</TabControl>
The visual studio shows error in the code behind says "ColorOption does not exist in the current context"
How can I fix this? Is any way to set the context? thank you
You can simply wrap the StackPanel in ContentControl and make it ControlTemplate.
<ControlTemplate x:Key="ColorOptionTemplate" TargetType="{x:Type ContentControl}">
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
</Button.Content>
</Button>
</StackPanel>
</ControlTemplate>
However, you will need to change properties of controls inside the ContentControl and it would be cumbersome. So the StackPanel could be wrapped in UserControl instead.
<UserControl x:Class="WpfApp1.ColorOptionControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel x:Name="ColorOption" Visibility="Collapsed">
<TextBlock Text="Line Color" Style="{StaticResource ItemNameTextBlockStyle}"/>
<Button Style="{StaticResource ColorButtonStyle}" Click="Color_Click">
<Button.Content>
<Rectangle x:Name="LineColorRect" Style="{StaticResource ColorSelectionRectangleStyle}"/>
</Button.Content>
</Button>
</StackPanel>
</UserControl>
This is a common way in WPF. The downside is that you will need to add dependency properties to UserControl and wire up them with dependency properties of internal controls so that you can set their values at the level of UserControl and bridge them with external controls and window. This could be complicated and cumbersome as well.
So I think ideally it would be better to find an existing control which has similar functionalities you want and create a custom control deriving from the existing one.

Calendar control width is too large at initialization of usercontrol

I have used calendar control and its display mode is year when my user control is loads then calender width s too large and doesn't appear when width is set .but when is select any month then width is set as it should
i m not getting what is the issue behind it
can anyone help me quickly???
Here is my code and what ever selected month will appear in textbox's text property and on click event of button my calendar control is set to visible
<StackPanel Grid.Row="0" Grid.Column="0" Orientation="HorizontalHorizontalAlignment="Left "Margin="4">
<Label Content="Month: " HorizontalAlignment="Right" VerticalAlignment="Top" FontSize="15" FontWeight="DemiBold" Margin="4"/>
<TextBox Height="23" HorizontalAlignment="Stretch" Name="SelectedMonthTextBox" VerticalAlignment="Top" Width="130" Margin="4" LostFocus="SelectedMonthTextBox_LostFocus" SelectionBrush="#FF767879"/>
<Button Content=".." Height="20" Width="20" Margin="4" Name="button" Click="button_Click" />
</StackPanel>
<Calendar Grid.Row="1" Grid.Column="0" Margin="50,-8" Panel.ZIndex="100" Name="calendar1" DisplayModeChanged="calendar1_DisplayModeChanged" DisplayDateChanged="calendar1_DisplayDateChanged" HorizontalAlignment="Left" DisplayMode="Year" Style="{StaticResource CalendarStyle}" Focusable="False" HorizontalContentAlignment="Left" VerticalAlignment="Top" />
i took a look your code and from information you provided so far, there is no any change a you describe. I believe that you should check following:
What happens in calendar1_DisplayModeChanged
How defined your style: StaticResource CalendarStyle
If it possible please post them here. I will try to help as soon as possible :)

Creating Popup programmatically in WP7

How to create the popup in program? e.g. I need to rename the file in phone App. How to do this by using popup in wp7?
Add a Popup element to your XAML and define the content using regular elements (as you would any other Page or UserControl. Set the Popup.IsOpen property to true to show the Popup and false to close the Popup. The following XAML shows an example that I use for showing in-application "toast" notifications with the Silverlight Windows Phone Toolkit
<Popup x:Name="_toast">
<Grid x:Name="_toastContainer"
VerticalAlignment="Bottom"
Width="{Binding ActualWidth, ElementName=LayoutRoot}">
<StackPanel Margin="14,10">
<TextBlock Text="{Binding Title}"
HorizontalAlignment="Stretch"
TextWrapping="Wrap" />
<TextBlock Text="{Binding Content}"
HorizontalAlignment="Stretch"
TextWrapping="Wrap" />
</StackPanel>
</Grid>
</Popup>
Take a look at the Input Prompt control in the Coding4Fun Windows Phone Toolkit

Change content of ImageButton programatically (WPF)

I am using a ImageButton as
<Button Width="80" Height="25"
VerticalAlignment="Top"
HorizontalAlignment="Right"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Margin="0,0,1.5,0"
Name="btnActivate"
Click="OnActivate">
<StackPanel Orientation="Horizontal" Margin="3">
<Image Source="Shutdown.ico"
Width="12" Height="12"
Margin="0,0,5,0"/>
<TextBlock>Activate</TextBlock>
</StackPanel>
</Button>
I want to change the Content of button to 'Deactivate' when I click on it without changing the image. How can I do that? And also I need to some operations based on the content of button in my C# code.
You could just add a name to the TextBlock
<TextBlock Name="textBlock1">Activate</TextBlock>
and use it to modify the content in your OnActivate event handler
textBlock1.Text = "Deactivate";

How to dynamically add controls to a Silverlight Button?

I'm working on a Silverlight Polling control. My goal is to have a Button that contains other controls, so that when a user clicks on this combination, stuff happens.
Here's the basic XAML:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
<StackPanel Grid.Row="1" Grid.Column="0" Height="Auto" Width="Auto" Style="{StaticResource StackPnl}">
<TextBox Text="this is a text box" Style="{StaticResource Answer}" />
<ProgressBar Style="{StaticResource PollProgress}" />
</StackPanel>
</Button>
That works fine, but assume I don't have the StackPanel, etc, defined:
<Button Grid.Row="1" Grid.Column="0" Style="{StaticResource AnswerButton}">
</Button>
But that I want to add the StackPanel, TextBox, and ProgressBar at runtime.
Looking at just the first hurdle that I've run into, how do I dynamically add the StackPanel to the Button control?
Alternatively, if someone knows of a better way to do this...
var panel = new StackPanel();
Button.Content = panel;

Resources