更新:2007 年 11 月
命名空间:
System.Windows.Controls 程序集:
PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
Public Class Button _
Inherits ButtonBase
public class Button : ButtonBase
public ref class Button : public ButtonBase
public class Button extends ButtonBase
public class Button extends ButtonBase
<Button>
Content
</Button>
Button 类直接继承自 System.Windows.Controls.Primitives..::.ButtonBase 类。
内容模型:Button 是一个 ContentControl。其内容属性为 Content。有关 Button 的内容模型的更多信息,请参见控件内容模型概述。
在用户单击 Button 时处理 ButtonBase..::.Click 事件以进行响应。
OnMouseLeftButtonDown 方法将 MouseLeftButtonDown 事件标记为已处理。若要响应 MouseLeftButtonDown 事件,请将一个事件处理程序附加到 PreviewMouseLeftButtonDown 事件,或者调用 AddHandler(RoutedEvent, Delegate, Boolean),其中 handledEventsToo 设置为 true。
下面的示例演示以三种不同方式对单击进行响应的三个按钮。
悬停:第一个按钮在用户将鼠标悬停在该按钮上时改变颜色。
按下:第二个按钮要求当鼠标指针位于该按钮上时按下鼠标。
释放:第三个按钮不重置按钮的背景颜色,直到在该按钮上按下并释放鼠标。
<Button Name="btn1" Background="Pink"
BorderBrush="Black" BorderThickness="1"
Click="OnClick1" ClickMode="Hover">
ClickMe1
</Button>
<Button Name="btn2" Background="LightBlue"
BorderBrush="Black" BorderThickness="1"
Click="OnClick2" ClickMode="Press">
ClickMe2
</Button>
<Button Name="btn3"
Click="OnClick3" ClickMode="Release">
Reset
</Button>
Private Sub OnClick1(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Background = Brushes.LightBlue
End Sub
Private Sub OnClick2(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn2.Background = Brushes.Pink
End Sub
Private Sub OnClick3(ByVal sender As Object, ByVal e As RoutedEventArgs)
btn1.Background = Brushes.Pink
btn2.Background = Brushes.LightBlue
End Sub
void OnClick1(object sender, RoutedEventArgs e)
{
btn1.Background = Brushes.LightBlue;
}
void OnClick2(object sender, RoutedEventArgs e)
{
btn2.Background = Brushes.Pink;
}
void OnClick3(object sender, RoutedEventArgs e)
{
btn1.Background = Brushes.Pink;
btn2.Background = Brushes.LightBlue;
}
更多代码
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows.Media..::.Visual
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Control
System.Windows.Controls..::.ContentControl
System.Windows.Controls.Primitives..::.ButtonBase
System.Windows.Controls..::.Button
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源