WPF简单UI菜单设计
UI效果如下:
XAML 设计:
<Window x:Class="简单菜单设计.MainWindow"
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:简单菜单设计"
mc:Ignorable="d"
Title="UI简单菜单设计" Height="450" Width="800">
<Window.CommandBindings>
<CommandBinding Command="New" CanExecute="CommandBinding_CanExecute"/>
<CommandBinding Command="Open" CanExecute="CommandBinding_CanExecute"/>
<CommandBinding Command="Save" CanExecute="CommandBinding_CanExecute"/>
</Window.CommandBindings>
<Grid>
<DockPanel >
<ToolBarTray DockPanel.Dock="Top">
<ToolBar>
<Button Command="New" Margin="2">
<StackPanel >
<Image Source="pack://application:,,,/Images/New.png" Height="40" Width="40"></Image>
<TextBlock HorizontalAlignment="Center">New</TextBlock>
</StackPanel>
</Button>
<Button Command="Open" Margin="2">
<StackPanel>
<Image Source="pack://application:,,,/Images/Open.png" Height="40" Width="40"></Image>
<TextBlock VerticalAlignment="Center">Open</TextBlock>
</StackPanel>
</Button>
<Button Command="Save" Margin="2">
<StackPanel>
<Image Source="pack://application:,,,/Images/Save.png" Height="40" Width="40"></Image>
<TextBlock VerticalAlignment="Center">Save</TextBlock>
</StackPanel>
</Button>
</ToolBar>
<ToolBar>
<Button Command="Copy" Content="Copy" ToolBar.OverflowMode="AsNeeded"/>
<Button Command="Paste" Content="Paste" ToolBar.OverflowMode="Never"/>
<Button Command="Cut" ToolTip="Cut selection to windows Clipboards" Content="Cut" ToolBar.OverflowMode="Always"></Button>
<Separator/>
<Label VerticalAlignment="Center">Font Size:</Label>
<ComboBox >
<ComboBoxItem>10</ComboBoxItem>
<ComboBoxItem>11</ComboBoxItem>
<ComboBoxItem IsSelected="True">12</ComboBoxItem>
<ComboBoxItem>13</ComboBoxItem>
<ComboBoxItem>14</ComboBoxItem>
<ComboBoxItem>15</ComboBoxItem>
<ComboBoxItem>16</ComboBoxItem>
</ComboBox>
</ToolBar>
</ToolBarTray>
<StatusBar DockPanel.Dock="Bottom" Margin="2" >
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<TextBlock x:Name="txtPosition"></TextBlock>
<Separator Grid.Column="1" />
<StatusBarItem Grid.Column="2">
<TextBlock Text="状态栏工具测试"/>
</StatusBarItem>
<Separator Grid.Column="3" />
<StatusBarItem Grid.Column="4">
<ProgressBar Value="50" Width="90" Height="16" />
</StatusBarItem>
</StatusBar>
<TextBox x:Name="txtEditor" AcceptsReturn="True" Margin="5" SelectionChanged="txtEditor_SelectionChanged"></TextBox>
</DockPanel>
</Grid>
</Window>
后台代码设计:
namespace 简单菜单设计
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
private void txtEditor_SelectionChanged(object sender, RoutedEventArgs e)
{
int row = txtEditor.GetLineIndexFromCharacterIndex(txtEditor.CaretIndex);
int col = txtEditor.CaretIndex - txtEditor.GetCharacterIndexFromLineIndex(row);
txtPosition.Text = "Line " + (row + 1) + ", Char " + (col);
}
}
}