【问题标题】:Change button background Activated MainWindow with C#使用 C# 更改按钮背景激活的 MainWindow
【发布时间】:2021-03-06 11:43:41
【问题描述】:

我正在学习 c# (Visual Studio 2019)。在 MainWindow 中,我有一个带有 page1.xaml 的框架。这个page1 有几个按钮。在激活 MainWindow 时,我想将 page1 中一个按钮的背景更改为红色。我怎么能做到这一点? 提前感谢您的任何回答 萨宾

【问题讨论】:

  • 你试过了吗?
  • 请提供一些关于您迄今为止尝试过的信息。这将很容易检查。 :-)

标签: c# wpf visual-studio


【解决方案1】:

您必须为按钮提供名称(例如 button1),在 WPF 按钮背景中需要为 SolidColorBrush 而不是直接颜色,所以当页面加载时,在 InitializeComponent 之后的 PageFunction 中添加此代码();

        SolidColorBrush brush = new SolidColorBrush(Colors.Red);// convert color to brush            
        button1.Backgroud=brush;// apply the brush

【讨论】:

    【解决方案2】:

    谢谢。但该按钮不在 MainWindow 中。该按钮位于页面 SubMenueSammlung.xaml 中名为“SubMenue”的框架中。

    MainWindow.xaml:

    <DockPanel LastChildFill="True">
        <!--Page SubMenueSammlung.xaml-->
        <Grid Background="#FFF2F2F2" DockPanel.Dock="Top" Height="24">
            <Frame x:Name="SubMenue" NavigationUIVisibility="Hidden"/>            
        </Grid>
    
        <!--Content-->
        <Grid>
            <Frame x:Name="Main" NavigationUIVisibility="Hidden"/>
        </Grid>
    </DockPanel>
    

    在 MainWindow.xaml.cs 中激活 MainWindow:

    public void FillFrame(object sender, EventArgs e)
        {
            SubMenue.Content = new SubMenueSammlung();
        }
    

    MainWindow 打开,Frame SubMenue 获取页面“SubMenueSammlung.xaml”。而现在我不知道下一步该怎么做...... 我希望框架中“SubMenueSammlung”页面上的按钮具有红色背景。

    我有这个按钮的样式:

        <Style TargetType="Button" x:Key="redButton" BasedOn="{StaticResource baseButton}">
            <Setter Property="Background" Value="#FF900000"/>
            <Setter Property="Foreground" Value="White"/>
            <Setter Property="FontWeight" Value="Bold"/>
        </Style>
    

    谢谢萨宾

    请原谅我的英语,我是德国女人

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2021-11-26
      • 2018-03-18
      相关资源
      最近更新 更多