This is a migrated thread and some comments may be shown as answers.

Selected Item not displayed

4 Answers 353 Views
ComboBox
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 29 Apr 2011, 09:44 AM
When displaying "Name" from button collection, selected item is not displayed.
It looks pretty straight forward but I can't think of why it won't work.. hmm..
Is this a telerik bug?
The following code shows two different methods, "DataTemplate" and "DisplayMemberPath"
I appreciate for your help.
-chris


<Window x:Class="RadComboBoxExample.MainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="grid">
        <Grid.Resources>
            <DataTemplate x:Key="comboTemplate">
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
          
        <StackPanel Grid.Column="0" Orientation="Vertical">
            <TextBlock Text="Use ItemTemplate" Margin="10" FontSize="15" HorizontalAlignment="Center" />
            <telerik:RadComboBox x:Name="comboBox1" Width="100" Height="30"  
                                 ItemsSource="{Binding ButtonCollection1}" 
                                 SelectedItem="{Binding SelectedButton1}" 
                                 ItemTemplate="{StaticResource comboTemplate}"/>
        </StackPanel>
  
        <StackPanel Grid.Column="1" Orientation="Vertical">
            <TextBlock Text="Use DisplayMemberPath" Margin="10" FontSize="15" HorizontalAlignment="Center" />
            <telerik:RadComboBox x:Name="comboBox2" Width="100" Height="30" 
                                 Grid.Column="1"
                                 ItemsSource="{Binding ButtonCollection2}" 
                                 SelectedItem="{Binding SelectedButton2}" 
                                 DisplayMemberPath="Name"/>
        </StackPanel>
    </Grid>
</Window>

using System.Collections.ObjectModel;
using System.Windows;
using Telerik.Windows.Controls;
  
namespace RadComboBoxExample
{
    public partial class MainWindow : Window
    {
        public ObservableCollection<RadButton> ButtonCollection1 { get; set; }
        public ObservableCollection<RadButton> ButtonCollection2 { get; set; }
        public RadButton SelectedButton1 { get; set; }
        public RadButton SelectedButton2 { get; set; }
  
        public MainWindow()
        {
            InitializeComponent();
  
            ButtonCollection1 = new ObservableCollection<RadButton>();
            ButtonCollection2 = new ObservableCollection<RadButton>();
  
            for (int i = 0; i < 10; i++)
            {
                RadButton button1 = new RadButton();
                button1.Name = "Button1_Item" + i;
                ButtonCollection1.Add(button1);
  
                RadButton button2 = new RadButton();
                button2.Name = "Button2_Item" + i;
                ButtonCollection2.Add(button2);
            }
  
            grid.DataContext = this;
        }
    }
}

4 Answers, 1 is accepted

Sort by
0
Yana
Telerik team
answered on 04 May 2011, 12:32 PM
Hello Chris,

It's not recommended to load UI elements in the view model. Please replace RadButton with a custom object in order to fix this issue. I've attached a simple example to demonstrate the approach.

Kind regards,
Yana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chris
Top achievements
Rank 1
answered on 04 May 2011, 01:24 PM
Thanks Yana,
I was aware of non-ui-element works as in your sample.
But I wasn't quite sure why it doesn't work for ui-element types, such as Button?
There is a reason why I'm using a button.
I would like to know at least why it doens't support. It doesn't seem logical to me.  hmm..

Thanks.

-chris
0
Yana
Telerik team
answered on 06 May 2011, 09:24 AM
Hello Chris,

I'm sorry for not explaining.

We follow the behavior of standard ComboBox.  If you replace RadComboBox with ComboBox in your example, you'll observe the same result.  I suggest to place the button in the ItemTemplate of the combobox. You can also set SelectionBoxTemplate as explained here.

Greetings,
Yana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chris
Top achievements
Rank 1
answered on 06 May 2011, 09:53 AM
Thanks for trying to help
You are right, normal wpf behavior is the same but that still doesn't explain why it shouldn't work.
It looks more like a bug than a feature. ^^

Regards,

-chris
Tags
ComboBox
Asked by
Chris
Top achievements
Rank 1
Answers by
Yana
Telerik team
Chris
Top achievements
Rank 1
Share this question
or