【问题标题】:Change displayed text in XAML Combobox multi selection更改 XAML 组合框多选中显示的文本
【发布时间】:2017-01-25 06:02:26
【问题描述】:

我有以下代码允许用户从组合框中选择多个项目。但是,当他们单击一项时,它会在组合框关闭时使其成为显示的文本。我可以将显示的文本更改为不仅仅是所选项目的内容吗?例如,如果用户选择项目 A、B 和 D,我希望组合框的文本部分显示“A、B、D”

<ComboBox ItemsSource="{Binding ListOfItems}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="20" />
                        <TextBlock Text="{Binding DisplayName}" Width="110" />
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

谢谢

【问题讨论】:

    标签: wpf xaml checkbox mvvm


    【解决方案1】:

    您可以使用带有 Style 的 ContentControl 来更改所选项目的 ContentTemplate 属性。以下示例标记应该会给您一些想法。

    <ComboBox ItemsSource="{Binding ListOfItems}">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <ContentControl Content="{Binding}">
                    <ContentControl.Style>
                        <Style TargetType="ContentControl">
                            <Setter Property="ContentTemplate">
                                <Setter.Value>
                                    <!-- the template for the items in the dropdown list -->
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay}" Width="20" />
                                            <TextBlock Text="{Binding DisplayName}" Width="110" />
                                        </StackPanel>
                                    </DataTemplate>
                                </Setter.Value>
                            </Setter>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType=ComboBoxItem}}" Value="{x:Null}">
                                    <Setter Property="ContentTemplate">
                                        <Setter.Value>
                                            <!-- the template for the selected item-->
                                            <DataTemplate>
                                                <ItemsControl ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType=ComboBox}}">
                                                    <ItemsControl.ItemsPanel>
                                                        <ItemsPanelTemplate>
                                                            <WrapPanel />
                                                        </ItemsPanelTemplate>
                                                    </ItemsControl.ItemsPanel>
                                                    <ItemsControl.ItemTemplate>
                                                        <DataTemplate>
                                                            <TextBlock Text="{Binding DisplayName}" Margin="0 0 5 0"/>
                                                        </DataTemplate>
                                                    </ItemsControl.ItemTemplate>
                                                </ItemsControl>
                                            </DataTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </ContentControl.Style>
                </ContentControl>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
    

    请参考以下类似问题了解更多信息。

    Can I use a different Template for the selected item in a WPF ComboBox than for the items in the dropdown part?

    【讨论】:

    • 谢谢。但我想知道的是如何绑定到不是所选项目的属性。我有 2 个属性 - 下拉列表中显示的项目列表,然后是一个字符串属性,它结合了所有选定的项目,我希望显示它而不是选定的项目。
    • 我最终使用 RelativeSource 绑定到数据上下文中的值 -
    猜你喜欢
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多