WPF中如何设置自定义控件(四)

在上一节讲到过将自定义界面进行左右分割,左边作为登录的用户信息和菜单;右边作为详情展示界面。

这节准备在上一节的基础上对左侧部分进行细分,完善功能,左侧的内容准备做两部分填充:一是登录用户的信息,一是菜单。因此需要将左侧的<Grid>表格控件分割成两行,地一行方式用户信息;第二行放置菜单。

<Window x:Class="DataGrid.Window1"

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:Icon ="http://metro.mahapps.com/winfx/xaml/iconpacks"

xmlns:local="clr-namespace:DataGrid"

mc:Ignorable="d"

Title="Window1" Height="450" Width="800">

<Window.Resources>

<BooleanToVisibilityConverter x:Key="BoolToVis" />

</Window.Resources>

<Border CornerRadius="30" Background="#EFF2F7" MouseDown="Border_MouseDown" MouseLeftButtonDown="Border_MouseLeftButtonDown">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="200"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<!--Left Menu-->

<Border Background="#623ED0" CornerRadius="0 50 0 30">

<Grid Grid.Column="0">

<Grid.RowDefinitions>

<RowDefinition/>

<RowDefinition/>

</Grid.RowDefinitions>

<!--用户信息-->

<Grid Grid.Row="0">

</Grid>

<!--菜单-->

<Grid Grid.Row="1" >

</Grid>

</Grid>

</Border>

<!--Main Section-->

<Border Grid.Column="1" Background="Aquamarine" CornerRadius="5">

<Grid Grid.Column="1" Margin="30 20 20 20">

</Grid>

</Border>

</Grid>

</Border>

</Window>

为了尽一步完善信息,我决定在用户信息中加载用户照片,显示用户名称和地址;在第二个<Grid>中添加菜单(暂且全用TextBlock代替)。由于左侧的大小是固定长度200,因此所有内容选择竖直排列。代码如下:

<Window x:Class="DataGrid.Window1"

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:Icon ="http://metro.mahapps.com/winfx/xaml/iconpacks"

xmlns:local="clr-namespace:DataGrid"

mc:Ignorable="d"

Title="Window1" Height="450" Width="800">

<Window.Resources>

<BooleanToVisibilityConverter x:Key="BoolToVis" />

</Window.Resources>

<Border CornerRadius="30" Background="#EFF2F7" MouseDown="Border_MouseDown" MouseLeftButtonDown="Border_MouseLeftButtonDown">

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="200"/>

<ColumnDefinition Width="*"/>

</Grid.ColumnDefinitions>

<!--Left Menu-->

<Border Background="#623ED0" CornerRadius="0 50 0 30">

<Grid Grid.Column="0">

<Grid.RowDefinitions>

<RowDefinition/>

<RowDefinition/>

</Grid.RowDefinitions>

<Grid Grid.Row="0">

<Border Background="#623ED0" Margin="0 0 0 60" Height="160" Width="80">

<StackPanel Orientation="Vertical">

<Ellipse Height="80" Width="80">

<Ellipse.Fill>

<ImageBrush ImageSource="/Images/p1.jpg"/>

</Ellipse.Fill>

</Ellipse>

<TextBlock Foreground="White" Text="James" Height="40" ></TextBlock>

<TextBlock Foreground="White" Text="西部大道"></TextBlock>

</StackPanel>

</Border>

</Grid>

<!--菜单-->

<Grid Grid.Row="1" >

<StackPanel Orientation="Vertical">

<TextBlock Foreground="White"> 菜单1</TextBlock>

<TextBlock Foreground="White"> 菜单2</TextBlock>

<TextBlock Foreground="White"> 菜单3</TextBlock>

<TextBlock Foreground="White"> 菜单4</TextBlock>

<TextBlock Foreground="White"> 菜单5</TextBlock>

</StackPanel>

</Grid>

</Grid>

</Border>

<!--Main Section-->

<Border Grid.Column="1" Background="Aquamarine" CornerRadius="5">

<Grid Grid.Column="1" Margin="30 20 20 20">

</Grid>

</Border>

</Grid>

</Border>

</Window>

效果图:

相关推荐
bugcome_com19 小时前
WPF样式进阶实战:外置样式+MVVM主题切换+样式优先级全解析
c#·.net·wpf
lalala_Zou1 天前
场景题:电商平台订单未支付过期如何实现自动关闭订单?
wpf
czhc11400756631 天前
wpf 16
wpf
cn_mengbei2 天前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos
lingxiao168882 天前
WebApi详解+Unity注入--上篇:基于Framework的WebApi
c#·wpf·web
是一个Bug2 天前
Java后端开发面试题清单(50道) - 分布式基础
java·分布式·wpf
无心水2 天前
【分布式利器:腾讯TSF】4、TSF配置中心深度解析:微服务动态配置的终极解决方案
分布式·微服务·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
lingxiao168883 天前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水3 天前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨4 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对