简体   繁体   English

在组合框中显示文本

[英]Display Text in the Combobox

I have an wpf application.I want to show the selected item in the Combo box.我有一个 wpf 应用程序。我想在组合框中显示所选项目。 I get an error saying Cant use both DisplayMemberPath and Item Template.我收到一条错误消息,说不能同时使用 DisplayMemberPath 和项目模板。

My ItemsSource is not of string type its a class called "StockExchange"我的 ItemsSource 不是字符串类型,它是一个名为“StockExchange”的类

Following is my code :以下是我的代码:

 <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>

what is solution for this?什么是解决方案? how can I display single or multiple selected Items in a Combo box?如何在组合框中显示单个或多个选定项目?

 <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>

Create a Textblock that Shows the selected item StockExchangeName创建一个显示所选项目 StockExchangeName 的文本块

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

If you select CheckBox, no item is shown selected in comboBox because no item is selected since click event is handled by checkbox.如果选择 CheckBox,则在 comboBox 中不会显示选中的项目,因为由于单击事件由复选框处理,因此没有选中任何项目。

You can bind IsChecked with IsSelected value of ComboBoxItem so that on click of checkbox corresponding item gets selected.您可以将 IsChecked 与 ComboBoxItem 的 IsSelected 值绑定,以便在单击复选框时选择相应的项目。

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

This will show checkBox in comboBox because you have provided template with checkBox in it for comboBoxItem.这将在组合框中显示复选框,因为您已经为组合框项目提供了带有复选框的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM