🏆 作者简介,愚公搬代码 🏆《头衔》:华为云特约编辑,华为云云享专家,华为开发者专家,华为产品云测专家,CSDN博客专家,阿里云专家博主,腾讯云优秀博主,掘金优秀博主,51CTO博客专家等。 🏆《近期荣誉》:2022年CSDN博客之星TOP2,2022年华为云十佳博主等。 🏆《博客内容》:.NET、Java、Python、Go、Node、前端、IOS、Android、鸿蒙、Linux、物联网、网络安全、大数据、人工智能、U3D游戏、小程序等相关领域知识。 🏆🎉欢迎 👍点赞✍评论⭐收藏
WPF控件是Windows Presentation Foundation(WPF)中的基本用户界面元素。它们是可视化对象,可以用来创建各种用户界面。WPF控件可以分为两类:原生控件和自定义控件。
原生控件是由Microsoft提供的内置控件,如Button、TextBox、Label、ComboBox等。这些控件都是WPF中常见的标准用户界面元素。
自定义控件则允许开发人员使用XAML和C#等编程语言来创建个性化的用户界面元素。自定义控件可以根据需求提供更多的功能和自定义化选项,以及更好的用户体验。
在WPF中,ToolBar控件是一个非常常用的控件。ToolBar控件通常用来放置一系列可以执行命令的按钮或其他控件,常见的应用场景是如Microsoft Office等办公软件中的工具栏。
以下是ToolBar控件的一些常用属性:
以下是一个简单的例子,展示如何在XAML中使用ToolBar控件:
<ToolBar Orientation="Horizontal">
<Button Content="New" />
<Button Content="Open" />
<Button Content="Save" />
</ToolBar>
在上面的例子中,我们创建了一个水平排列的ToolBar控件,并在其中添加了三个按钮,分别对应“新建”、“打开”和“保存”操作。
ToolBar控件是WPF中常用的控件之一,以下是ToolBar控件常用属性的介绍:
<Window x:Class="WpfAppTest.ToolBarWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTest"
mc:Ignorable="d"
Title="ToolBarWindow" Height="450" Width="800">
<Window.Resources>
<RoutedUICommand x:Key="setColor"/>
</Window.Resources>
<Window.CommandBindings>
<CommandBinding Command="{StaticResource setColor}" Executed="SetLabelColor"/>
</Window.CommandBindings>
<Grid>
<ToolBar HorizontalAlignment="Left" Height="36" Margin="10,10,0,0" VerticalAlignment="Top" Width="207" IsOverflowOpen="True" >
<Button Name="btnNew" Content="新建" Click="BtnNew_Click"/>
<Separator/>
<Image Source="imgs/1111.jpg" Height="20" Width="20"/>
<CheckBox Content=" 是否保存" IsChecked="True" Margin="10,0"/>
<Separator/>
<RadioButton Name="rbRed" ToolTip="Red" Command="{StaticResource setColor}">
<RadioButton.Content>
<Rectangle Width="10" Height="10" Fill="Red"/>
</RadioButton.Content>
</RadioButton>
<RadioButton ToolTip="Yellow" Command="{StaticResource setColor}" >
<RadioButton.Content>
<Rectangle Width="10" Height="10" Fill="Yellow"/>
</RadioButton.Content>
</RadioButton>
<RadioButton ToolTip="Green" Command="{StaticResource setColor}">
<RadioButton.Content>
<Rectangle Width="10" Height="10" Fill="Green"/>
</RadioButton.Content>
</RadioButton>
<RadioButton ToolTip="Purple" Command="{StaticResource setColor}">
<RadioButton.Content>
<Rectangle Width="10" Height="10" Fill="Purple"/>
</RadioButton.Content>
</RadioButton>
<Separator/>
<ComboBoxItem Content="选择" VerticalContentAlignment="Center" PreviewMouseLeftButtonUp="ComboBoxItem_MouseLeftButtonUp"/>
</ToolBar>
<Label Name="lbl" Content="我是测试文本" HorizontalAlignment="Left" Margin="103,78,0,0" VerticalAlignment="Top"/>
</Grid>
</Window>
/// <summary>
/// 设置文本颜色
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SetLabelColor(object sender,RoutedEventArgs e)
{
RadioButton rb = e.Source as RadioButton;
Rectangle rect = rb.Content as Rectangle;
lbl.Foreground = rect.Fill;
}
private void ComboBoxItem_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
lbl.Content = "罗丽";
}
private void BtnNew_Click(object sender, RoutedEventArgs e)
{
Window newWin = new Window();
newWin.Title = "新建页面";
newWin.Show();
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。