在Combobox中显示文本

时间:2014-08-11 12:25:38

标签: c# wpf combobox telerik

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

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

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

2 个答案:

答案 0 :(得分:0)

 <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}" />

答案 1 :(得分:0)

如果选择CheckBox,则在comboBox中未显示任何项目,因为没有选择任何项目,因为click事件由复选框处理。

您可以 使用ComSeBoxItem的IsSelected值绑定IsChecked ,以便点击复选框时相应的项目会被选中。

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

这将在comboBox中显示checkBox,因为你已经为comboBoxItem提供了带有checkBox的模板。