c# - WPF如何制作一个自定义组合框,里面有一个按钮来清除
问题描述
我做了一个简单的组合框,里面有一个删除按钮。如果用户单击删除按钮,则组合框的 selectedItem 将为空。
这是我原来的组合框:(我知道,堆栈面板应该在一个按钮内)
<TextBlock Text="ComboBox:"></TextBlock>
<ComboBox
ItemsSource="{Binding ITEMS}"
SelectedItem="{Binding Item}"
DisplayMemberPath="Description"
IsTextSearchEnabled="True"
IsEditable="False"
IsDropDownOpen="False">
</ComboBox>
<StackPanel
Orientation="Horizontal"
HorizontalAlignment="Right"
MouseEnter="GridSplitter_MouseEnter"
MouseLeave="GridSplitter_MouseLeave">
<Image
Source="../Images/clos.png"
Height="12" Width="15" Margin="0 0 20 4">
</Image>
</StackPanel>
我的问题:如何制作自定义组合框元素,这样我就不必为必须制作的每个组合框重复此代码。并且清除的值是 selectedItem。
它会是这样的:
<custom:ComboBoxWithClearance>
ItemsSource="{Binding ITEMS}"
SelectedItem="{Binding Item}"
DisplayMemberPath="Description"
IsTextSearchEnabled="True"
IsEditable="False"
IsDropDownOpen="False"
</custom:ComboBoxWithClearance>
解决方案
您正在谈论创建 UserControl
推荐阅读
- javascript - 如何以编程方式获取 chrome 扩展 ID
- javascript - 如何获取firestore中子集合的实时更新?
- lean - 精益定义中的类型问题
- java - 使用camera2获取图像捕获时间
- linux - OSX 和 Linux 之间的 Grep 差异
- pandas - 熊猫:摆脱特定多索引级别的时间戳
- c# - C# 将类型转换为具有相同声明的不同类型
- javascript - 如何从函数访问数据库然后才发送发布请求?
- javascript - 根据第二个 .then 的条件从第一个 .then 获取数据
- angular - Angular 重新登录导致 Firefox 中的持续重定向