簡體   English   中英

按鈕設計的問題。 材料設計, c#, wpf

[英]Problem with button designs. MaterialDesign, c#, wpf

我的問題是,我想用MaterialDesign制作一個菜單,當我將 go 更改為菜單中的菜單項之一時,我希望按鈕樣式發生變化。 但是,當我覆蓋 MaterialDesign 樣式時,我無法重置它。 我想做這樣的事情:在這里輸入圖片描述

那是我的菜單代碼:

    <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>

我的 c# 代碼:

 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;
        }

這就是現在的樣子:在此處輸入圖片描述

首先,您需要在所有頁面中執行此操作:

MainWindow mw = null;
public Employees(MainWindow mw)
{
    InitializeComponent();
    this.mw = mw;
}

為您的 MainWindow 創建一個 null object,然后為您的 Page 方法提供一個參數,然后在該方法中,您像我在代碼中向您展示的那樣聲明 MainWindow。

完成此操作后,您需要將 go 發送到 MainWindow.xaml.cs,您必須執行以下操作:

public MainWindow()
{
    InitializeComponent();
    employees = new Employees(this);
    Presenter.Content = employees;
}

Employees employees = null;

為您的頁面創建一個 null object,並且在 MainWindow 方法中您必須像這樣聲明頁面。

之后,您只需要在頁面上加載一個 Loaded 和一個 Unloaded 事件:

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");
}

你可以使用任何你想要的顏色,我希望它有幫助:D

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM