Wpf 更改XAML组合框多选中显示的文本

Wpf 更改XAML组合框多选中显示的文本,wpf,xaml,checkbox,mvvm,Wpf,Xaml,Checkbox,Mvvm,我有以下代码允许用户从组合框中选择多个项目。但是,当他们单击一个项目时,当combobox关闭时,它会使其成为显示的文本。我可以将显示的文本更改为不只是所选项目的内容吗。例如,如果用户选择项目A、B和D,我希望组合框的文本部分显示“A、B、D” 谢谢您可以将ContentControl与更改所选项目的ContentTemplate属性的样式一起使用。下面的示例标记应该会告诉您这个想法 <ComboBox ItemsSource="{Binding ListOfItems}">

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



谢谢您可以将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>

有关更多信息,请参考以下类似问题


谢谢你的帮助。但我想知道的是,我如何绑定到一个不是所选项的属性。我有两个属性-下拉列表中显示的项目列表,但有一个字符串属性组合了所有选定的项目,我希望显示该属性而不是选定的项目。我最终使用RelativeSource绑定到数据上下文中的值-
<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>