在组合框中显示文本

本文关键字:显示 文本 组合 | 更新日期: 2023-09-27 18:17:39

我有一个wpf应用程序。我想在组合框中显示选中的项目。我得到一个错误说不能同时使用DisplayMemberPath和Item模板。

我的ItemsSource不是字符串类型它是一个叫做"StockExchange"的类

下面是我的代码:
 <telerik:RadComboBox Grid.Column="1" DisplayMemberPath="StockExchangeName"  Name="cmbStockExchange" Foreground="White" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="118,14,0,0" VerticalAlignment="Top" Width="100" Height="23" ItemsSource="{Binding StockExchange, Mode=TwoWay}" SelectedItem="{Binding SelectedStockExchange,Mode= TwoWay}" telerik:StyleManager.Theme="Summer" TabIndex="3">
                    <telerik:RadComboBox.ItemTemplate >
                        <DataTemplate>
                            <CheckBox Name="StockExchange"  Content="{Binding StockExchangeName}">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <Commands:EventToCommand Command="{Binding DataContext.StockExchangeCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
                                    </i:EventTrigger>
                                    <i:EventTrigger EventName="Unchecked">
                                        <Commands:EventToCommand Command="{Binding DataContext.StockExchangeUnCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
                                    </i:EventTrigger>                                  
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </DataTemplate>
                    </telerik:RadComboBox.ItemTemplate>
  </telerik:RadComboBox>

这个问题的解决方案是什么?如何在组合框中显示单个或多个选定项目?

在组合框中显示文本

 <telerik:RadComboBox Grid.Column="1"   Name="cmbStockExchange" Foreground="White" HorizontalAlignment="Left" HorizontalContentAlignment="Center" Margin="118,14,0,0" VerticalAlignment="Top" Width="100" Height="23" ItemsSource="{Binding StockExchange, Mode=TwoWay}" SelectedItem="{Binding SelectedStockExchange,Mode= TwoWay}" telerik:StyleManager.Theme="Summer" TabIndex="3">
                <telerik:RadComboBox.ItemTemplate >
                    <DataTemplate>
                        <CheckBox Name="StockExchange"  Content="{Binding StockExchangeName}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Checked">
                                    <Commands:EventToCommand Command="{Binding DataContext.StockExchangeCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
                                </i:EventTrigger>
                                <i:EventTrigger EventName="Unchecked">
                                    <Commands:EventToCommand Command="{Binding DataContext.StockExchangeUnCheckedCmd,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerik:RadWindow}}}" CommandParameter="{Binding ElementName=StockExchange}" ></Commands:EventToCommand>
                                </i:EventTrigger>                                  
                            </i:Interaction.Triggers>
                        </CheckBox>
                    </DataTemplate>
                </telerik:RadComboBox.ItemTemplate>

创建一个文本块,显示选中的项目StockExchangeName

<TextBlock Text="{Binding Path=SelectedItem.StockExchangeName, ElementName=cmbStockExchange}" />

如果您选择了CheckBox,则在comboBox中没有显示选中的项,因为没有项被选中,因为单击事件是由CheckBox处理的。

你可以绑定IsChecked和ComboBoxItem的IsSelected值,这样当点击复选框对应的项被选中

<CheckBox Content="{Binding StockExchangeName}"
            IsChecked="{Binding IsSelected, RelativeSource={RelativeSource 
                                Mode=FindAncestor, AncestorType=ComboBoxItem}}"/>

这将在comboBox中显示复选框,因为您已经为comboBoxItem提供了带有复选框的模板。