如何更改所有按钮

时间:2015-09-22 12:28:09

标签: c# wpf

我正在开发一个C#WPF项目(VS 2010),它有一些不同形式的按钮,并希望为所有这些按钮设置一些常用属性(例如:粗体文本,悬停时更改颜色)。但是,我可以使用以下代码逐一设置属性选项卡中的属性和悬停行为。

private void btnOne_MouseHover(object sender, EventArgs e)
{           
    (sender as Button).BackColor = Color.Orange;
}

 private void btnTwo_MouseLeave(object sender, EventArgs e)
 {         
     (sender as Button).BackColor = Color.LightGray;
 }

有没有办法从一个地方更改所有按钮属性?有任何可用的例子吗?

2 个答案:

答案 0 :(得分:2)

在WPF中,您可以在XAML中执行此操作。

.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            // current activity
                            // --------------------------
                            // here you need to dismiss the dialog
                             dialog.dismiss();
                            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
                            startActivity(browserIntent);

                        }
                      })

答案 1 :(得分:1)

我对WPF的体验并不是很棒但是你可以将MouseHover和MouseLeave设置为XAML样式(我从头开始写这个而不是在IDE中,因为我在手机上,所以它可能不完全正确):

<Style x:Key="MyStyle" TargetType="Button">
    <Setter Property="Background" Value="GRAY_COLOUR" />
        <Style.Triggers>
          <Trigger Property="IsMouseOver" Value="True">
              <Setter Property="Background" Value="ORANGE_COLOUR" />
          </Trigger>
        </Style.Triggers>
</Style>

GRAY_COLOURORANGE_COLOUR替换为该颜色的十六进制值,或将它们定义为静态资源。然后将该样式分配给所有适用的按钮:

<Button Style="{StaticResource MyStyle}" />