propertyGrid控件 z

本文介绍在使用enum类型作为属性时如何实现下拉显示,并讲解了自定义下拉数据的方法,涉及TypeConverter的使用。
好的,下面是一个简单的WPF自定义下拉框控件的实现: 1. 创建一个新的WPF控件项目,命名为“CustomComboBox”。 2. 在项目中添加一个新的类,命名为“CustomComboBox.cs”,在类中添加以下代码: ``` using System.Windows; using System.Windows.Controls; namespace CustomComboBox { public class CustomComboBox : ComboBox { static CustomComboBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomComboBox), new FrameworkPropertyMetadata(typeof(CustomComboBox))); } } } ``` 这里我们继承了WPF内置的`ComboBox`控件,并重写了`DefaultStyleKeyProperty`属性,这是自定义控件的必要步骤之一。 3. 在项目中添加一个新的资源字典,命名为“Generic.xaml”,并添加以下代码: ``` <Style TargetType="{x:Type local:CustomComboBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:CustomComboBox}"> <Grid> <ToggleButton x:Name="PART_ToggleButton" Grid.Column="2" Focusable="false" IsChecked="{Binding Path=IsDropDownOpen, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="20" /> </Grid.ColumnDefinitions> <Border x:Name="Border" Grid.ColumnSpan="2" CornerRadius="0" Background="White" BorderBrush="Black" BorderThickness="1" /> <Path x:Name="Arrow" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 4 4 L 8 0 Z" /> </Grid> </ToggleButton> <Popup x:Name="PART_Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True" Focusable="False" PopupAnimation="Slide"> <Border x:Name="PopupBorder" Background="White" BorderBrush="Black" BorderThickness="1" CornerRadius="0"> <ScrollViewer x:Name="ScrollViewer" SnapsToDevicePixels="True"> <StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained" /> </ScrollViewer> </Border> </Popup> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="False"> <Setter Property="Foreground" Value="#888888" /> </Trigger> <Trigger Property="IsGrouping" Value="True"> <Setter Property="ScrollViewer.CanContentScroll" Value="False" /> </Trigger> <Trigger SourceName="PopupBorder" Property="BorderThickness" Value="0"> <Setter TargetName="Border" Property="Margin" Value="0" /> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> ``` 这里定义了一个名为“CustomComboBox”的样式,它继承自WPF内置的`ComboBox`样式,并在其中添加了一个`ToggleButton`控件,用于展开/收起下拉框,以及一个`Popup`控件,用于显示下拉框中的内容。 4. 在项目中添加一个新的XAML文件,命名为“MainWindow.xaml”,并添加以下代码: ``` <Window x:Class="CustomComboBox.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:CustomComboBox" Title="MainWindow" Height="350" Width="525"> <Grid> <local:CustomComboBox Width="150" Height="30"> <ComboBoxItem>Item 1</ComboBoxItem> <ComboBoxItem>Item 2</ComboBoxItem> <ComboBoxItem>Item 3</ComboBoxItem> </local:CustomComboBox> </Grid> </Window> ``` 这里我们在窗口中添加了一个自定义下拉框控件,并在其中添加了三个选项。 5. 运行程序,你将看到一个自定义的下拉框控件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值