Skip to main content

Microsoft Silverlight

Answered Question Missing SelectedValue and SelectedValuePath properties in ComboBoxRSS Feed

(0)

sd@sercl.com
sd@sercl...

Member

Member

6 points

25 Posts

Missing SelectedValue and SelectedValuePath properties in ComboBox

Hi,

 I'm very surprise to not find the SelectedValue and SelectedValuePath properties in the new ComboBox control. Why this missing and how to do to databind it ?

Fantome91

HarshBardhan
HarshBar...

Star

Star

9908 points

1,719 Posts

Answered Question

Re: Missing SelectedValue and SelectedValuePath properties in ComboBox

Hi,

you can bind data like this.

//Page.Xaml

  <StackPanel x:Name="LayoutRoot" Background="White">
        <ComboBox x:Name="mycomboBox" ItemsSource="{Binding}" SelectionChanged="mycomboBox_SelectionChanged" Height="50"></ComboBox>

    <ComboBox x:Name="Listbox"   Visibility="Visible" Height="50">


            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding  Str}"  />
                        <TextBlock Text="Details"  />
                    </StackPanel>

                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

 

    </StackPanel>

//Page.xaml.cs

      public Page()
        {
            InitializeComponent();
            List<Name> name = new List<Name>();
            name.Add(new Name { Str = "Harsh" });
            name.Add(new Name { Str = "HarshBardhan" });
            name.Add(new Name { Str = "HarshBardhanHarsh" });
            name.Add(new Name { Str = "Harsh" });


            var Query = from c1 in name
                        // where c1.Str == "Harsh"
                        select new Name { Str = c1.Str };

           Listbox.SelectedIndex = -1;
           Listbox.ItemsSource = Query;
mycomboBox.DataContext=Query;
mycomboBox.DisplayMemberPath="Str";

     }
        public class Name
        {
            private string str;

            public string Str
            {
                get { return str; }
                set { str = value; }
            }
        }

   

        private void mycomboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
Name a =mycomboBox.SelectedItem as Name;

        }
        }
    }


 

 

Mark as answer if this post answered your question.

Harsh Bardhan

spiderman110
spiderma...

Member

Member

220 points

276 Posts

Re: Re: Missing SelectedValue and SelectedValuePath properties in ComboBox

 Hi, Harsh.I have one problem with your sample.

I just add a button and in button's click event I write like below:

private void BtnOK_Click(object sender, RoutedEventArgs e)
        {
           
            ObservableCollection<Name> name = new ObservableCollection<Name>();
            name.Add(new Name { Str = "Harsh" });
            name.Add(new Name { Str = "HarshBardhan" });
            name.Add(new Name { Str = "HarshBardhanHarsh" });
            name.Add(new Name { Str = "Harsh" });


            var Query = from c1 in name
                        //where c1.Str == "Harsh"
                        select new Name { Str = c1.Str };

            mycomboBox.DataContext = Query;
            mycomboBox.DisplayMemberPath = "Str";
            mycomboBox.SelectedIndex = 0;

        }

 I changed the selectedIndex from -1 to 0, and when i change the selectedItem of mycomboBox and click the button, error occurs. I know there is something wrong with selectedIndex, but i really want the selectedIndex to be 0. 

any suggustions? 

Thanks

 

ZeekJones
ZeekJones

Member

Member

4 points

5 Posts

Re: Missing SelectedValue and SelectedValuePath properties in ComboBox

mahendranvs
mahendranvs

Member

Member

8 points

6 Posts

Re: Missing SelectedValue and SelectedValuePath properties in ComboBox

While setting the selected item of silverlight combo box, there is an issue. I have tried updatelayout and finally i have inherited and created a new combo control. Then overided the method PrepareContainerForItemOverride and call base method with try catch which works fine. See my post below for sample code.

http://silverlight.net/forums/p/103044/234997.aspx#234997

Cheers,
Mahendran Shanmugham
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities