My problem would be, I want to make a menu with MaterialDesign and I want the button style to change when I go to one of the menu items in the menu. However, when I overwrite the MaterialDesign style, I cannot reset it. I wanna make somethin like like this:enter image description here
Thats my menu code:
<Grid>
<StackPanel Orientation="Horizontal" Height="35" VerticalAlignment="Top" >
<Button x:Name="User_B" Content="Users" MinWidth="100" Click="User_B_Click"/>
<Button x:Name="Auto_B" Content="Auto" MinWidth="100" Click="Auto_B_Click"/>
<Button x:Name="Clien_B" Content="Client" MinWidth="100" Click="Clien_B_Click" />
<Button x:Name="Failure_B" Content="Failure" MinWidth="100" Click="Failure_B_Click"/>
<Button x:Name="Settings_B" Content="Settings" MinWidth="100" Click="Settings_B_Click"/>
</StackPanel>
<Frame x:Name="Main" Margin="0,35,0,0" NavigationUIVisibility="Hidden"/>
</Grid>
My c# code:
private void User_B_Click(object sender, RoutedEventArgs e)
{
Style selectstyle = new Style();
selectstyle.TargetType = typeof(Button);
selectstyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#303030"))));
selectstyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#d50000"))));
User_B.Style = selectstyle;
Auto_B.Style = null;
}
private void Auto_B_Click(object sender, RoutedEventArgs e)
{
Style selectstyle = new Style();
selectstyle.TargetType = typeof(Button);
selectstyle.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#303030"))));
selectstyle.Setters.Add(new Setter(Button.ForegroundProperty, new SolidColorBrush((Color)ColorConverter.ConvertFromString("#d50000"))));
Auto_B.Style = selectstyle;
User_B.Style = null;
}
and this is how it looks now:enter image description here
CodePudding user response:
First you need to do this in all of your pages :
MainWindow mw = null;
public Employees(MainWindow mw)
{
InitializeComponent();
this.mw = mw;
}
Create a null object for your MainWindow , than give a parameter to your Page method , then in the method , you declare the MainWindow like I show you in the code.
After you done with this , then you need to go to the MainWindow.xaml.cs and you have to do this:
public MainWindow()
{
InitializeComponent();
employees = new Employees(this);
Presenter.Content = employees;
}
Employees employees = null;
Create a null object for your Page , and in the MainWindow method you have to declare the Page like this.
After that , you just need a Loaded and an Unloaded event to your page :
private void Page_Loaded(object sender, RoutedEventArgs e)
{
mw.EmployeesButton.Background = (Brush)new BrushConverter().ConvertFrom("#006B60");
}
private void Page_Unloaded(object sender, RoutedEventArgs e)
{
mw.EmployeesButton.Background = (Brush)new BrushConverter().ConvertFrom("#009688");
}
You can use any color you want , I hope its helps :D