CheckComboBox:如何阻止组合框在选择被点击后关闭? [英] CheckComboBox : How to prevent a combobox from closing after a selection is clicked?

查看:207
本文介绍了CheckComboBox:如何阻止组合框在选择被点击后关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个组合框,它有一堆复选框。我希望用户能够在组合框关闭之前多次点击(或者由用户自己关闭)。现在的问题是,每次一个复选框被点击,组合框关闭,迫使想要选择多个选项的用户重新打开它多次。



I



我已尝试设置 StayOpenOnEdit 但这没有做的伎俩。到目前为止我看不到,没有财产解决我的问题。


  1. 如何防止combobox在点击后自动关闭



  2. 是否有更适合此类任务的组件?如果是,它的名称是什么? b

    请注意,即使#2的答案是是,我仍然对于纯粹的学术上的原因感到好奇。



    我已经玩了切换按钮和列表框,如某人建议在一篇文章。但是,这只导致一个完全显示的所有复选框的列表,在它后面有一些灰色的东西(我假设是toggler)。

     < ToggleButton Horizo​​ntalAlignment =Left
    Margin = 550,62,0,0
    VerticalAlignment =Top
    Width =100>
    < ListBox x:Name =listBox1
    Horizo​​ntalAlignment =Left
    Height =100
    VerticalAlignment =TopWidth =100>
    < CheckBox x:Name =checkBox3Content =CheckBox/>
    < CheckBox x:Name =checkBox4Content =CheckBox/>
    < / ListBox>
    < / ToggleButton>

    关键是要实现这样的东西,但它需要一个标准的WPF控件(所有选定项目的并置行是好的,但不是必须的)。

    我已经阅读了一些有关绑定和处理尚未完全开发的投诉,使用 Exapander控制,您可以实现多个项目选择,而无需在单个选择后关闭弹出式窗口。



    xaml

     < Window.Resources> 
    < ControlTemplate x:Key =ComboboxToggleButtonTargetType ={x:Type ToggleButton}>
    < Grid Background ={Binding Background,RelativeSource = {RelativeSource AncestorType = {x:Type ComboBox}}}>
    < Grid.ColumnDefinitions>
    < ColumnDefinition />
    < ColumnDefinition Width =35/>
    < /Grid.ColumnDefinitions>
    < Border x:Name =BorderGrid.ColumnSpan =2Background =TransparentBorderBrush =BlackBorderThickness ={Binding BorderThickness,RelativeSource = {RelativeSource TemplatedParent}}
    < Path x:Name =ArrowGrid.Column =1Opacity =1Stroke =BlackStrokeThickness =1.5Horizo​​ntalAlignment =CenterVerticalAlignment =CenterData =M 0 0 L 6 6 L 12 0/>
    < Path x:Name =Arrow_checkedOpacity =0Grid.Column =1Fill =BlackStroke =BlackStrokeThickness =1Horizo​​ntalAlignment =CenterVerticalAlignment =Center Data =M 0 0 L 6 6 L 12 0 Z/>
    < ContentPresenter TextElement.FontFamily =Segoe Ui DarkTextElement.FontSize =18TextElement.Foreground =BlackVerticalAlignment =CenterGrid.Column =0Margin =10,0,0 ,0Horizo​​ntalAlignment =LeftRecognizingAccessKey =TrueSnapsToDevicePixels =True/>
    < / Grid>
    < ControlTemplate.Triggers>
    < Trigger Property =ToggleButton.IsMouseOverValue =true>
    < Setter TargetName =BorderProperty =BackgroundValue =Gray/>
    < Setter TargetName =BorderProperty =BorderThicknessValue =1.2/>
    < / Trigger>
    < Trigger Property =IsCheckedValue =False>
    < Setter Property =OpacityValue =1TargetName =Arrow/>
    < Setter Property =OpacityValue =0TargetName =Arrow_checked/>
    < / Trigger>
    < Trigger Property =ToggleButton.IsCheckedValue =true>
    < Setter Property =OpacityValue =0TargetName =Arrow/>
    < Setter Property =OpacityValue =1TargetName =Arrow_checked/>
    < / Trigger>
    < Trigger属性=IsEnabledValue =False>
    < Setter TargetName =BorderProperty =BackgroundValue =Gray/>
    < Setter TargetName =BorderProperty =BorderBrushValue =White/>
    < / Trigger>
    < /ControlTemplate.Triggers>
    < / ControlTemplate>

    < Style TargetType ={x:Type Expander}>
    < Setter Property =FontFamilyValue =Segoe Ui Dark>< / Setter>
    < Setter Property =Horizo​​ntalContentAlignmentValue =Stretch/>
    < Setter Property =VerticalContentAlignmentValue =Stretch/>
    < Setter Property =MaxHeightValue =200>< / Setter>
    < Setter Property =Template>
    < Setter.Value>
    < ControlTemplate TargetType ={x:Type Expander}>
    < DockPanel> TemplateBinding HeaderContentTemplate ={TemplateBinding HeaderTemplate}ContentTemplateSelector ={TemplateBinding HeaderTemplateSelector}Content ={TemplateBinding Header}ContentTemplate ={TemplateBinding HeaderTemplate} DockPanel.Dock =TopForeground =BlackFontStyle ={TemplateBinding FontStyle}FontFamily =Segoe UI Dark
    IsChecked ={Binding IsExpanded,Mode = TwoWay,RelativeSource = {RelativeSource TemplatedParent}} Template ={StaticResource ComboboxToggleButton}/>
    < Border BorderThickness =0,45,0,0BorderBrush =Transparent>
    < Border x:Name =bodBorderBrush =TransparentSnapsToDevicePixels =TrueBorderThickness =1>
    < ContentPresenter X:NAME =ExpandSite调焦=假的Horizo​​ntalAlignment ={TemplateBinding Horizo​​ntalContentAlignment}能见度=坍塌VerticalAlignment ={TemplateBinding VerticalContentAlignment}/>
    < / Border>
    < / Border>
    < / DockPanel>
    < ControlTemplate.Triggers>
    < Trigger Property =IsExpandedValue =true>
    < Setter Property =VisibilityTargetName =ExpandSiteValue =Visible/>
    < Setter Property =BorderBrushTargetName =bodValue =Black/>
    < / Trigger>
    < Trigger Property =IsEnabledValue =false>
    < Setter Property =ForegroundValue =Gray>< / Setter>
    < / Trigger>
    < /ControlTemplate.Triggers>
    < / ControlTemplate>
    < /Setter.Value>
    < / Setter>
    < / Style>

    < Style x:Key =itemstyleTargetType ={x:Type ListBoxItem}>
    < Setter Property =SnapsToDevicePixelsValue =true/>
    < Setter Property =OverridesDefaultStyleValue =true/>
    < Setter Property =Template>
    < Setter.Value>
    < ControlTemplate TargetType =ListBoxItem>
    < Grid x:Name =BorderHeight =40SnapsToDevicePixels =true>
    < Grid.Background>
    < SolidColorBrush Color =Transparent/>
    < /Grid.Background>
    < ContentPresenter NAME =cmb_nameTextElement.FontFamily =濑越暗UITextElement.FontSize =18TextElement.Foreground =黑保证金=10,0,0,0VerticalAlignment =中心>< / ContentPresenter>
    < / Grid>
    < ControlTemplate.Triggers>
    < Trigger Property =IsMouseOverValue =True>
    < Setter TargetName =BorderProperty =BackgroundValue =Gray>< / Setter>
    < / Trigger>
    < /ControlTemplate.Triggers>
    < / ControlTemplate>
    < /Setter.Value>
    < / Setter>
    < Setter Property =FocusVisualStyle>
    < Setter.Value>
    < Style TargetType =Control>
    < Setter Property =Template>
    < Setter.Value>
    < ControlTemplate>
    < Border BorderBrush ={DynamicResource customBlueBrush}BorderThickness =1Margin =1,2,2,2>
    < Rectangle Fill ={DynamicResource customBlueBrush}Opacity =0.1>< / Rectangle>
    < / Border>
    < / ControlTemplate>
    < /Setter.Value>
    < / Setter>
    < / Style>
    < /Setter.Value>
    < / Setter>
    < / Style>

    < Style TargetType =CheckBox>
    < Setter Property =SnapsToDevicePixelsValue =True>< / Setter>
    < Setter Property =OverridesDefaultStyleValue =True>< / Setter>
    < Setter Property =Template>
    < Setter.Value>
    < ControlTemplate TargetType =CheckBox>
    < Grid x:Name =abBackground =透明>
    < Grid.ColumnDefinitions>
    < ColumnDefinition Width =30/>
    < ColumnDefinition />
    < /Grid.ColumnDefinitions>
    <矩形VerticalAlignment =中心HEIGHT =20WIDTH =20填充=白的Horizo​​ntalAlignment =左>< /矩形>
    < Grid x:Name =checkGridVerticalAlignment =CenterHeight =20Width =20Background =BlackHorizo​​ntalAlignment =Left>
    < Viewbox Height =13Width =13>
    <路径X:NAME =检查SnapsToDevicePixels =真UseLayoutRounding =真WIDTH =18.7969HEIGHT =16.3094Canvas.Left =0Canvas.Top =1.52588e-005 拉伸=填充填充=白色数据=F1中号0.731262,8.75935L 0.106262,8.08437C 0.0354614,7.9948 0,7.8979 0,7.79375C 0,7.66875 0.0479736,7.5573 0.143799,7.45937L 1.94067,5.77187C 2.02606, 5.69893 2.12708,5.66249 2.24377,5.66249C 2.30212,5.66249 2.36096,5.67397 2.42035,5.69685C 2.47974,5.71977 2.52814,5.75417 2.56567,5.79997L 7.5188,11.1406L 16.0438,0.165604C 16.1417,0.055191 16.2584,1.52588e-005 16.3938,1.52588e- 005C 16.4979,1.52588e-005 16.5896,0.0322723 16.6688,0.0968475L 18.6313,1.60939C 18.6709,1.64272 18.7084,1.69011 18.7438,1.75154C 18.7792,1.813 18.7969,1.8698 18.7969,1.92189C 18.7969,2.03435 18.7646,2.1385 18.7,2.23439L 7.74377, 16.3094L 0.731262,8.75935 Z/>
    < / Viewbox>
    < / Grid>
    < Grid Background =TransparentGrid.Column =1IsHitTestVisible =TrueHorizo​​ntalAlignment =Stretch>
    < TextBlock的VerticalAlignment =中心字号=18的FontFamily =濑越暗UI前景=黑TextTrimming =CharacterEllipsis>
    < ContentPresenter>< / ContentPresenter>
    < / TextBlock>
    < / Grid>

    < / Grid>
    < ControlTemplate.Triggers>
    < Trigger Property =IsCheckedValue =True>
    < Setter Property =OpacityValue =1TargetName =Check>< / Setter>
    < / Trigger>
    < Trigger Property =IsCheckedValue =False>
    < Setter Property =OpacityValue =0TargetName =Check>< / Setter>
    < / Trigger>
    < /ControlTemplate.Triggers>
    < / ControlTemplate>
    < /Setter.Value>
    < / Setter>
    < Setter Property =FocusVisualStyle>
    < Setter.Value>
    < Style TargetType =Control>
    < Setter Property =Template>
    < Setter.Value>
    < ControlTemplate>
    < Border BorderBrush ={DynamicResource customBlueBrush}SnapsToDevicePixels =TrueBorderThickness =1Margin = - 5,1,3,1/>
    < / ControlTemplate>
    < /Setter.Value>
    < / Setter>
    < / Style>
    < /Setter.Value>
    < / Setter>
    < / Style>
    < /Window.Resources>

    < Grid x:Name =gd>
    < Expander Width =500>
    < Expander.Header>
    < ListBox Background =TransparentIsHitTestVisible =FalseBorderBrush =TransparentScrollViewer.Horizo​​ntalScrollBarVisibility =HiddenScrollViewer.VerticalScrollBarVisibility =DisabledBorderThickness =0ItemsSource ={Binding ElementName = Path = SelectedItems}>
    < ListBox.ItemsPanel>
    < ItemsPanelTemplate>
    < WrapPanel Orientation =Horizo​​ntal>< / WrapPanel>
    < / ItemsPanelTemplate>
    < /ListBox.ItemsPanel>
    < ListBox.ItemTemplate>
    < DataTemplate>
    < TextBlock Text ={Binding ContentData}/>
    < / DataTemplate>
    < /ListBox.ItemTemplate>
    < / ListBox>
    < /Expander.Header>
    < Expander.Content>
    < ListBox Background =TransparentItemContainerStyle ={StaticResource itemstyle}Horizo​​ntalContentAlignment =Stretchx:Name =lstSelectionMode =Multiple>
    < ListBox.ItemTemplate>
    < DataTemplate>
    < CheckBox x:Name =checkBoxIsChecked ={Binding RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type ListBoxItem}},Path = IsSelected}Content ={Binding ContentData}& < / CheckBox>
    < / DataTemplate>
    < /ListBox.ItemTemplate>
    < / ListBox>
    < /Expander.Content>
    < / Expander>
    < / Grid>

    c#c​​ode

      public partial class Window1:Window 
    {
    public Window1()
    {
    InitializeComponent
    ObservableCollection< Customer> custdata = new ObservableCollection< Customer>();
    custdata.Add(new Customer(){ContentData =content1});
    custdata.Add(new Customer(){ContentData =content2});
    custdata.Add(new Customer(){ContentData =content3});
    custdata.Add(new Customer(){ContentData =content4});
    custdata.Add(new Customer(){ContentData =content5});
    custdata.Add(new Customer(){ContentData =content6});
    lst.ItemsSource = custdata;
    }
    }
    public class Customer
    {
    public string ContentData {get;组; }
    }

    结果 b


    I've got a combobox and it's populated with a bunch of checkboxes. I wish the user to be able to click multiple times before the combobox closes (or is closed by the user themselves). The problems right now is that each time a checkbox is clicked, the combobox closes, forcing the user who wants to select multiple options to re-open it several times.

    I've found other questions on the same subject but those apply to Silverlight, Qt etc. comparing the tags.

    I've tried setting StayOpenOnEdit but that didn't do the trick. As far I could see, there's no property addressing my issue. That creates a suspicion that I might be barning up the wrong component all together.

    1. How do I prevent the combobox to close automatically after a click in a checkbox in it?
    2. Is there a more suitable component for such task and, if so, what's its name?

    Please note that even if the answer to #2 is "yes", I'm still curious of #1 for purely academic reasons.

    I've played with toggle button and list box, as someone suggested in a post. However, the that led only to an always fully shown list of all the checkboxes with some greyish thing behind it (which I'm assuming is the toggler). Perhaps I did something less clever in the mark-up.

    <ToggleButton HorizontalAlignment="Left" 
                  Margin="550,62,0,0" 
                  VerticalAlignment="Top"
                  Width="100">
      <ListBox x:Name="listBox1" 
                 HorizontalAlignment="Left" 
                 Height="100"
                 VerticalAlignment="Top" Width="100">
        <CheckBox x:Name="checkBox3" Content="CheckBox"/>
        <CheckBox x:Name="checkBox4" Content="CheckBox"/>
      </ListBox>
    </ToggleButton>
    

    The point is to achieve something like this but it needs to be a standard WPF control (the concatenated line of all selected items is nice but not a must). Also, I've read the complaints that the binding and handling is not fully developed yet and I feel a bit suspicious.

    解决方案

    Using Exapander Control you can achieve multple item selection without closing popup after single selection.

    For understanding please run this code separately.

    xaml

      <Window.Resources>
        <ControlTemplate x:Key="ComboboxToggleButton" TargetType="{x:Type ToggleButton}">
            <Grid Background="{Binding Background,RelativeSource={RelativeSource AncestorType={x:Type ComboBox}}}">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="35" />
                </Grid.ColumnDefinitions>
                <Border x:Name="Border" Grid.ColumnSpan="2" Background="Transparent"  BorderBrush="Black" BorderThickness="{Binding BorderThickness,RelativeSource={RelativeSource TemplatedParent}}"/>
                <Path x:Name="Arrow" Grid.Column="1" Opacity="1" Stroke="Black" StrokeThickness="1.5" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 6 6 L 12 0" />
                <Path x:Name="Arrow_checked" Opacity="0" Grid.Column="1" Fill="Black" Stroke="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" Data="M 0 0 L 6 6 L 12 0 Z" />
                <ContentPresenter TextElement.FontFamily="Segoe Ui Dark" TextElement.FontSize="18" TextElement.Foreground="Black" VerticalAlignment="Center"  Grid.Column="0" Margin="10,0,0,0" HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="True" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ToggleButton.IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="Gray" />
                    <Setter TargetName="Border" Property="BorderThickness" Value="1.2" />
                </Trigger>
                <Trigger Property="IsChecked" Value="False">
                    <Setter Property="Opacity" Value="1" TargetName="Arrow"/>
                    <Setter Property="Opacity" Value="0" TargetName="Arrow_checked"/>
                </Trigger>
                <Trigger Property="ToggleButton.IsChecked" Value="true">
                    <Setter Property="Opacity" Value="0" TargetName="Arrow"/>
                    <Setter Property="Opacity" Value="1" TargetName="Arrow_checked"/>
                </Trigger>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="Gray" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="White" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    
        <Style TargetType="{x:Type Expander}">
            <Setter Property="FontFamily" Value="Segoe Ui Dark"></Setter>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            <Setter Property="VerticalContentAlignment" Value="Stretch"/>
            <Setter Property="MaxHeight" Value="200"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Expander}">
                        <DockPanel>
                            <ToggleButton x:Name="HeaderSite" Height="35" Background="{TemplateBinding Background}" ContentTemplate="{TemplateBinding HeaderTemplate}" ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" Content="{TemplateBinding Header}" DockPanel.Dock="Top" Foreground="Black"   FontStyle="{TemplateBinding FontStyle}"   FontFamily="Segoe UI Dark" 
                         IsChecked="{Binding IsExpanded, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"   Template="{StaticResource ComboboxToggleButton}" />
                            <Border  BorderThickness="0,4.5,0,0" BorderBrush="Transparent">
                                <Border x:Name="bod" BorderBrush="Transparent" SnapsToDevicePixels="True" BorderThickness="1">
                                    <ContentPresenter x:Name="ExpandSite"  Focusable="false" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"  Visibility="Collapsed" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                                </Border>
                            </Border>
                        </DockPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="true">
                                <Setter Property="Visibility" TargetName="ExpandSite" Value="Visible"/>
                                <Setter Property="BorderBrush" TargetName="bod" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="Gray"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style x:Key="itemstyle"   TargetType="{x:Type ListBoxItem}">
            <Setter Property="SnapsToDevicePixels" Value="true" />
            <Setter Property="OverridesDefaultStyle"  Value="true" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListBoxItem">
                        <Grid x:Name="Border"  Height="40"  SnapsToDevicePixels="true">
                            <Grid.Background>
                                <SolidColorBrush Color="Transparent" />
                            </Grid.Background>
                            <ContentPresenter Name="cmb_name" TextElement.FontFamily="Segoe Ui Dark" TextElement.FontSize="18" TextElement.Foreground="Black" Margin="10,0,0,0" VerticalAlignment="Center"></ContentPresenter>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Border" Property="Background" Value="Gray"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FocusVisualStyle">
                <Setter.Value>
                    <Style TargetType="Control">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Border BorderBrush="{DynamicResource customBlueBrush}" BorderThickness="1" Margin="1,2,2,2" >
                                        <Rectangle Fill="{DynamicResource customBlueBrush}" Opacity="0.1"></Rectangle>
                                    </Border>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    
        <Style  TargetType="CheckBox" >
            <Setter Property="SnapsToDevicePixels" Value="True"></Setter>
            <Setter Property="OverridesDefaultStyle" Value="True"></Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="CheckBox">
                        <Grid x:Name="ab" Background="Transparent">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="30"/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Rectangle VerticalAlignment="Center" Height="20" Width="20"  Fill="White" HorizontalAlignment="Left"></Rectangle>
                            <Grid x:Name="checkGrid" VerticalAlignment="Center" Height="20" Width="20" Background="Black" HorizontalAlignment="Left">
                                <Viewbox Height="13" Width="13">
                                    <Path x:Name="Check" SnapsToDevicePixels="True" UseLayoutRounding="True" Width="18.7969" Height="16.3094" Canvas.Left="0" Canvas.Top="1.52588e-005" Stretch="Fill" Fill="White" Data="F1 M 0.731262,8.75935L 0.106262,8.08437C 0.0354614,7.9948 0,7.8979 0,7.79375C 0,7.66875 0.0479736,7.5573 0.143799,7.45937L 1.94067,5.77187C 2.02606,5.69893 2.12708,5.66249 2.24377,5.66249C 2.30212,5.66249 2.36096,5.67397 2.42035,5.69685C 2.47974,5.71977 2.52814,5.75417 2.56567,5.79997L 7.5188,11.1406L 16.0438,0.165604C 16.1417,0.055191 16.2584,1.52588e-005 16.3938,1.52588e-005C 16.4979,1.52588e-005 16.5896,0.0322723 16.6688,0.0968475L 18.6313,1.60939C 18.6709,1.64272 18.7084,1.69011 18.7438,1.75154C 18.7792,1.813 18.7969,1.8698 18.7969,1.92189C 18.7969,2.03435 18.7646,2.1385 18.7,2.23439L 7.74377,16.3094L 0.731262,8.75935 Z " />
                                </Viewbox>
                            </Grid>
                            <Grid Background="Transparent" Grid.Column="1" IsHitTestVisible="True" HorizontalAlignment="Stretch">
                                <TextBlock VerticalAlignment="Center"  FontSize="18" FontFamily="Segoe Ui Dark" Foreground="Black"  TextTrimming="CharacterEllipsis">
                            <ContentPresenter></ContentPresenter>
                                </TextBlock>
                            </Grid>
    
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter Property="Opacity" Value="1"  TargetName="Check"></Setter>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False">
                                <Setter Property="Opacity" Value="0" TargetName="Check"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Setter Property="FocusVisualStyle">
                <Setter.Value>
                    <Style TargetType="Control">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Border BorderBrush="{DynamicResource customBlueBrush}" SnapsToDevicePixels="True" BorderThickness="1" Margin="-5,1,3,1" />
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    
    <Grid x:Name="gd" >
        <Expander Width="500">
            <Expander.Header>
                <ListBox Background="Transparent" IsHitTestVisible="False" BorderBrush="Transparent" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Disabled"  BorderThickness="0" ItemsSource="{Binding ElementName=lst,Path=SelectedItems}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel  Orientation="Horizontal"></WrapPanel>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding ContentData}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Expander.Header>
            <Expander.Content>
                <ListBox  Background="Transparent" ItemContainerStyle="{StaticResource itemstyle}" HorizontalContentAlignment="Stretch" x:Name="lst" SelectionMode="Multiple">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="checkBox"  IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Content="{Binding ContentData}"></CheckBox>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Expander.Content>
        </Expander>
    </Grid>
    

    c# code

      public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();          
            ObservableCollection<Customer> custdata = new ObservableCollection<Customer>();
            custdata.Add(new Customer() { ContentData = "content1" });
            custdata.Add(new Customer() { ContentData = "content2" });
            custdata.Add(new Customer() { ContentData = "content3" });
            custdata.Add(new Customer() { ContentData = "content4" });
            custdata.Add(new Customer() { ContentData = "content5" });
            custdata.Add(new Customer() { ContentData = "content6" });
            lst.ItemsSource = custdata;
        }
    }
    public class Customer
    {
        public string ContentData { get; set; }
    }
    

    Result

    这篇关于CheckComboBox:如何阻止组合框在选择被点击后关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆