如何拉伸复选框项目组合框的整个宽度

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何拉伸复选框项目组合框的整个宽度相关的知识,希望对你有一定的参考价值。

我有一个带复选框项目的多重组合框

<ComboBox  x:Name="cmb" IsEditable="True"  IsReadOnly="True" DropDownClosed="cmb_DropDownClosed">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
            <CheckBox Content="{Binding NmColumn }"  HorizontalAlignment="Stretch"
                      IsChecked="{Binding Path=bChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                      Tag="{Binding IdColumn}"
                      />
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

当我点击复选框时一切都很好。但是如果复选框宽度小于组合框的宽度,当我单击复选框右侧时,组合框关闭。如何覆盖此行为?

答案

HorizontalContentAlignmentStretch设为ComboBox.ItemContainerStyle

<ComboBox  x:Name="cmb" IsEditable="True"  IsReadOnly="True" DropDownClosed="cmb_DropDownClosed">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <CheckBox Content="{Binding NmColumn }"  HorizontalAlignment="Stretch"
                  IsChecked="{Binding Path=bChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                  Tag="{Binding IdColumn}"
                  />
                </StackPanel>
            </DataTemplate> 
        </ComboBox.ItemTemplate>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="{x:Type ComboBoxItem}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
另一答案

所有答案都不适合我。特技制作的是什么

Horizo​​ntalContentAlignment = “弹力”

对于CheckBox:

<ComboBox x:Name="combobox"
    Background="White"
    Padding="2"
    Text="{Binding ElementName=DockPanelTemplateComboCheck, Path=ComboTextFilter}"
    IsEditable="True"
    IsReadOnly="True"
    HorizontalAlignment="Stretch"
    ItemsSource="{Binding ...}"
    IsDropDownOpen="{Binding Path=DropOpen, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, UpdateSourceTrigger=PropertyChanged}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding Eintrag}" HorizontalContentAlignment="Stretch" Checked="CheckBox_Checked_Unchecked" Unchecked="CheckBox_Checked_Unchecked"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

代码隐藏:

    private string combotextfilter = "<No Selection>";

    public string ComboTextFilter
    {
        get { return combotextfilter; }
        set
        {
            if (value != null && value.IndexOf("ComboModel") != -1) return;
            combotextfilter = value;
            NotifyPropertyChanged(nameof(ComboTextFilter));
        }
    }

    private void CheckBox_Checked_Unchecked(object sender, RoutedEventArgs e)
    {
        switch (((ObservableCollection<ComboModel>)combobox.ItemsSource).Count(x => x.IsChecked))
        {
            case 0:
                ComboTextFilter = "<No Selection>";
                break;
            case 1:
                ComboTextFilter = ((ObservableCollection<ComboModel>)combobox.ItemsSource).Where(x => x.IsChecked).First().Eintrag;
                break;
            default:
                ComboTextFilter = ((ObservableCollection<ComboModel>)combobox.ItemsSource).Where(x => x.IsChecked).Select(x => x.Eintrag).Aggregate((i, j) => i + " | " + j);
                //ComboTextFilter = "<Multiple Selected>";
                break;
        }

        NotifyPropertyChanged(nameof(C_Foreground));
    }

    public bool DropOpen
    {
        get { return dropopen; }
        set { dropopen = value; NotifyPropertyChanged(nameof(ComboTextFilter)); }
    }
    private bool dropopen = false;

注意:此ComboBox还会根据所选元素更改ComboBox的文本。

另一答案

Ramin的答案部分是好的。你必须从你的数据模板中删除<StackPanel Orientation="Horizontal">。这是他限制你的复选框扩展所有ComboboxItem宽度。

<ComboBox  x:Name="cmb" IsEditable="True"  IsReadOnly="True" DropDownClosed="cmb_DropDownClosed">
    <ComboBox.ItemTemplate>
        <DataTemplate>

                <CheckBox Content="{Binding NmColumn }"  HorizontalAlignment="Stretch"
              IsChecked="{Binding Path=bChecked, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
              Tag="{Binding IdColumn}"
              />

        </DataTemplate> 
    </ComboBox.ItemTemplate>
    <ComboBox.ItemContainerStyle>
        <Style TargetType="{x:Type ComboBoxItem}">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ComboBox.ItemContainerStyle>
</ComboBox>

以上是关于如何拉伸复选框项目组合框的整个宽度的主要内容,如果未能解决你的问题,请参考以下文章

选择时如何让wpf listboxitem拉伸列表框的整个高度

如何在 WPF 中隐藏组合框的项目

如何使 WPF 数据模板填充列表框的整个宽度?

如何使用phpword将页脚拉伸到页面的整个宽度?

如何在 Flutter 中拉伸图像以适合整个背景(100% 高度 x 100% 宽度)?

如何设置 Dojo 组合框的宽度