WPF ComboBox清除按钮

时间:2018-08-06 13:08:06

标签: wpf combobox

我正在努力寻找如何在下面创建所需组合框的解决方案。如有必要,我可以使用其他控件类型来获得下图。

  1. 当选择一个项目时,如何使组合框中显示的文本保留为“名称”

  2. 如何在列表底部添加一个清除按钮,单击该按钮可以清除所有选中的项目?

enter image description here

<Window x:Class="WpfApplication1.MainWindow"
        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:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <XmlDataProvider x:Key="MockList"   XPath="/MockObjects/*" >
            <x:XData >
                <MockObjects xmlns="">
                    <MockObject  Name="Louis" Type="A" Number="1" />
                    <MockObject Name="Joseph Angelo" Type="A" Number="2" />
                    <MockObject Name="Papineau Philadelphia" Type="B" Number="301" />
                    <MockObject  Name="Mary" Type="B" Number="4" />
                    <MockObject Name="Kevin" Type="C" Number="5" />
                    <MockObject Name="Sarah" Type="D" Number="6" />
                    <MockObject  Name="Doug" Type="E" Number="7" />
                    <MockObject Name="Leslie" Type="F" Number="8" />
                    <MockObject Name="Mike" Type="G" Number="9" />
                    <MockObject Name="Sue" Type="H" Number="10" />
                    <MockObject  Name="Michelle" Type="I" Number="11" />
                    <MockObject Name="Ron" Type="J" Number="12" />
                    <MockObject Name="Amy" Type="K" Number="13" />
                </MockObjects>
            </x:XData>
        </XmlDataProvider>
    </Window.Resources>

    <Grid>
        <Button Content="Clear" VerticalAlignment="Top" Margin="80" Width="100" Height="24" />
        <ComboBox VerticalAlignment="Top" Margin="20" Text="Names" Width="100" Height="24" IsEditable="True" IsReadOnly="True" DataContext="{Binding Source={StaticResource MockList}}"  ItemsSource="{Binding Mode=Default, XPath=/MockObjects/MockObject}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsSelected}" Width="20" />
                        <TextBlock Text="{Binding XPath=@Name}" Width="100" />
                        <!--<TextBlock Text="{Binding DayOfWeek}" Width="100" />-->
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </Grid>
</Window>

0 个答案:

没有答案