C# WPF 实现侧边栏自动隐藏
By
jerryxjr1220
at 2023-10-07 • 0人收藏 • 565人看过
用HandyControl的Drawer实现

<hc:GlowWindow x:Class="WPFHandyControlDemo.Views.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:model="clr-namespace:WPFHandyControlDemo.Models"
xmlns:vm="clr-namespace:WPFHandyControlDemo.ViewModels"
xmlns:view="clr-namespace:WPFHandyControlDemo.Views"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
xmlns:local="clr-namespace:WPFHandyControlDemo"
xmlns:hc="https://handyorg.github.io/handycontrol"
d:DataContext="{d:DesignInstance Type=vm:MainViewModel}"
mc:Ignorable="d"
Title="Drawer Demo" Width="800" Height="450">
<hc:SimplePanel>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="30"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<RadioButton Margin="10" HorizontalAlignment="Stretch" Grid.Column="0" Content=" "
IsChecked="{Binding IsOpen,ElementName=DrawerLeft}" Style="{StaticResource RadioButtonSameAsButtonWarning}"/>
<Grid Grid.Column="1">
<Border BorderThickness="1" BorderBrush="DarkGray">
<TextBlock Text="Main Content" HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
</Border>
</Grid>
</Grid>
<hc:Drawer x:Name="DrawerLeft" Dock="Left" ShowMode="Push">
<Border Background="{DynamicResource RegionBrush}" Width="200" BorderThickness="0,1,0,0"
BorderBrush="{DynamicResource BorderBrush}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Margin="10,0,0,0" Text="Drawer SideBar" Style="{StaticResource TextBlockTitle}"
HorizontalAlignment="Left" />
<Button Grid.Row="0" HorizontalAlignment="Right" Click="ButtonBase_OnClick"
Foreground="{DynamicResource PrimaryTextBrush}" Style="{StaticResource ButtonIcon}"
hc:IconElement.Geometry="{StaticResource DeleteFillCircleGeometry}" />
<ListBox Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Top" Width="200">
<ListBoxItem>Item 1</ListBoxItem>
<ListBoxItem>Item 2</ListBoxItem>
</ListBox>
</Grid>
</Border>
</hc:Drawer>
</hc:SimplePanel>
</hc:GlowWindow>//侧边栏关闭按钮的点击事件
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
DrawerLeft.IsOpen = false;
}
1 个回复 | 最后更新于 2023-10-07
登录后方可回帖
老哥你对编程热情很高啊,我假期电脑都没开过