2021-08-23 WPF控件专题 ToolBar 控件详解

Posted 微软MVP Eleven

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2021-08-23 WPF控件专题 ToolBar 控件详解相关的知识,希望对你有一定的参考价值。

1.ToolBar 控件介绍

父类:HeaderedItemsControl MenuItem TreeViewItem —条目控件

属性:Orientation 指示排列方向(只读)
Band BandIndex IsOverflowOpen

ToolBarTray :呈放多个ToolBar 位置 索引位置

2.具体案例

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

以上是关于2021-08-23 WPF控件专题 ToolBar 控件详解的主要内容,如果未能解决你的问题,请参考以下文章

2021-08-23 WPF控件专题 ContextMenu 控件详解

2021-08-23 WPF控件专题 Menu控件详解

2021-08-13 WPF控件专题 ComboBox 控件详解

2021-08-19 WPF控件专题 TabControl 控件详解

2021-08-09 WPF控件专题 Button控件详解

2021-08-17 WPF控件专题 Groupbox 控件详解