首页 > 解决方案 > 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>

标签: c#wpfmvvm

解决方案



推荐阅读