wpf 实现为textbox添加下拉框
WPF中combox不能直接写入数据:需要实现功能:1.直接从键盘输入数据,2,具有下拉列表可供用户选择输入,用combox实现,textbox实现也可以,或者自定义控件...
WPF 中 combox 不能直接写入数据:需要实现功能:1.直接从键盘输入数据,2,具有下拉列表可供用户选择输入,用combox 实现,textbox实现也可以,或者自定义控件也可以
展开
1个回答
展开全部
1. 设置属性 IsEditable="True" Combobox就可编辑
2. 下拉框正常绑定数据就行,如果像上图每个Item都有个删除功能,那就需要重写样式,可参考下面的代码(带有动画效果,可删除)
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels"
Value="true"/>
<Setter
Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter
Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding"
Value="3"/>
<Setter Property="Focusable"
Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type ComboBoxItem}">
<Grid SnapsToDevicePixels="true" FocusVisualStyle="{x:Null}">
<Rectangle x:Name="BackgroundHighlight" FocusVisualStyle="{x:Null}"
Margin="-1" Stroke="#A0FFFFFF"/>
<ContentPresenter
Content="{Binding LoginName}" FocusVisualStyle="{x:Null}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
x:Name="contentPresenter" Margin="{TemplateBinding Padding}"/>
<Button Width="30"
Height="30" HorizontalAlignment="Right" Tag="{Binding LoginName}"
Style="{StaticResource CloseButton}" Click="BtnDeleteRecord_Click"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger
Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<BeginStoryboard
Storyboard="{StaticResource HoverOff}"
x:Name="HoverOff_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard
Storyboard="{StaticResource HoverOn}"
x:Name="HoverOn_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
<Trigger
Property="IsHighlighted" Value="true"/>
<Trigger
Property="Selector.IsSelected" Value="True">
<Trigger.ExitActions>
<BeginStoryboard
Storyboard="{StaticResource SelectedOff}"
x:Name="SelectedOff_BeginStoryboard1"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard
Storyboard="{StaticResource SelectedOn}"
x:Name="SelectedOn_BeginStoryboard1"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
2. 下拉框正常绑定数据就行,如果像上图每个Item都有个删除功能,那就需要重写样式,可参考下面的代码(带有动画效果,可删除)
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="SnapsToDevicePixels"
Value="true"/>
<Setter
Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter
Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Padding"
Value="3"/>
<Setter Property="Focusable"
Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="{x:Type ComboBoxItem}">
<Grid SnapsToDevicePixels="true" FocusVisualStyle="{x:Null}">
<Rectangle x:Name="BackgroundHighlight" FocusVisualStyle="{x:Null}"
Margin="-1" Stroke="#A0FFFFFF"/>
<ContentPresenter
Content="{Binding LoginName}" FocusVisualStyle="{x:Null}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
x:Name="contentPresenter" Margin="{TemplateBinding Padding}"/>
<Button Width="30"
Height="30" HorizontalAlignment="Right" Tag="{Binding LoginName}"
Style="{StaticResource CloseButton}" Click="BtnDeleteRecord_Click"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger
Property="IsMouseOver" Value="True">
<Trigger.ExitActions>
<BeginStoryboard
Storyboard="{StaticResource HoverOff}"
x:Name="HoverOff_BeginStoryboard"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard
Storyboard="{StaticResource HoverOn}"
x:Name="HoverOn_BeginStoryboard"/>
</Trigger.EnterActions>
</Trigger>
<Trigger
Property="IsHighlighted" Value="true"/>
<Trigger
Property="Selector.IsSelected" Value="True">
<Trigger.ExitActions>
<BeginStoryboard
Storyboard="{StaticResource SelectedOff}"
x:Name="SelectedOff_BeginStoryboard1"/>
</Trigger.ExitActions>
<Trigger.EnterActions>
<BeginStoryboard
Storyboard="{StaticResource SelectedOn}"
x:Name="SelectedOn_BeginStoryboard1"/>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
追问
这些代码写在哪个文件啊?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询