WPF ComboBox
-
Good afternoon. I cant seem to get a combobox to select a value using Binding. I have a small example that re-creates the issue, I want the combo to have selected Yellow (_colour is set to that on creation) when it first loads. Hope that makes sense Code is below, just window1 and a class called ViewModel. TIA for any help :) Window1.xaml
<window x:class="ComboBoxTest.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<grid>
<combobox margin="92,122,66,116">
ItemsSource="{Binding Path=Colours}"
DisplayMemberPath="Description"
SelectedItem="{Binding Path=SelectedColour, Mode=TwoWay}"
Grid.Column="1"
Grid.Row="8"
IsSynchronizedWithCurrentItem="True"/> </combobox></grid>
</window>
Window1.xaml.cs
using System.Windows;
namespace ComboBoxTest
{
/// /// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = new ViewModel();
InitializeComponent();
}
}
}ViewModel.cs
using System;
using System.Collections.Generic;
using System.Windows;namespace ComboBoxTest
{public class Colour : IComparable { public string Description { get; set; } #region IComparable Members public int CompareTo(object obj) { return string.Compare(((Colour) obj).Description, this.Description); } #endregion } public class ViewModel : DependencyObject { public List<colour> Colours { get { List<colour> retval = new List<colour>(); retval.Add(new Colour { Description = "Blue" }); retval.Add(new Colour { Description = "Green" }); retval.Add(new Colour { Description = "White" }); retval.Add(new Colour { Description = "Yellow" }); retval.Add(new Colour { Description = "Red" }); retval.Add(new Colour { Description = "Silver" });
-
Good afternoon. I cant seem to get a combobox to select a value using Binding. I have a small example that re-creates the issue, I want the combo to have selected Yellow (_colour is set to that on creation) when it first loads. Hope that makes sense Code is below, just window1 and a class called ViewModel. TIA for any help :) Window1.xaml
<window x:class="ComboBoxTest.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<grid>
<combobox margin="92,122,66,116">
ItemsSource="{Binding Path=Colours}"
DisplayMemberPath="Description"
SelectedItem="{Binding Path=SelectedColour, Mode=TwoWay}"
Grid.Column="1"
Grid.Row="8"
IsSynchronizedWithCurrentItem="True"/> </combobox></grid>
</window>
Window1.xaml.cs
using System.Windows;
namespace ComboBoxTest
{
/// /// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = new ViewModel();
InitializeComponent();
}
}
}ViewModel.cs
using System;
using System.Collections.Generic;
using System.Windows;namespace ComboBoxTest
{public class Colour : IComparable { public string Description { get; set; } #region IComparable Members public int CompareTo(object obj) { return string.Compare(((Colour) obj).Description, this.Description); } #endregion } public class ViewModel : DependencyObject { public List<colour> Colours { get { List<colour> retval = new List<colour>(); retval.Add(new Colour { Description = "Blue" }); retval.Add(new Colour { Description = "Green" }); retval.Add(new Colour { Description = "White" }); retval.Add(new Colour { Description = "Yellow" }); retval.Add(new Colour { Description = "Red" }); retval.Add(new Colour { Description = "Silver" });
The way you have declared SelectedColour isn't right, you don't need a backing field as it that is done automatically. You can either call GetValue/SetValue in the property passing in SelectedColourProperty or implement INotifyPropertyChanged and raise the event in the setter (Then you wouldn't need to inherit from DependencyObject). There may be some other issues as I couldn't test the code atm. http://msdn.microsoft.com/en-us/library/ms750428.aspx http://msdn.microsoft.com/en-us/library/ms229614.aspx
-
The way you have declared SelectedColour isn't right, you don't need a backing field as it that is done automatically. You can either call GetValue/SetValue in the property passing in SelectedColourProperty or implement INotifyPropertyChanged and raise the event in the setter (Then you wouldn't need to inherit from DependencyObject). There may be some other issues as I couldn't test the code atm. http://msdn.microsoft.com/en-us/library/ms750428.aspx http://msdn.microsoft.com/en-us/library/ms229614.aspx
-
Thanks for your reply, this was a test project and you are right i did not have the Selected Colour correct, i have changed it and it still does not work, so will look at INotifyPropertyChanged, and let you know. Mark
-
Good afternoon. I cant seem to get a combobox to select a value using Binding. I have a small example that re-creates the issue, I want the combo to have selected Yellow (_colour is set to that on creation) when it first loads. Hope that makes sense Code is below, just window1 and a class called ViewModel. TIA for any help :) Window1.xaml
<window x:class="ComboBoxTest.Window1" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<grid>
<combobox margin="92,122,66,116">
ItemsSource="{Binding Path=Colours}"
DisplayMemberPath="Description"
SelectedItem="{Binding Path=SelectedColour, Mode=TwoWay}"
Grid.Column="1"
Grid.Row="8"
IsSynchronizedWithCurrentItem="True"/> </combobox></grid>
</window>
Window1.xaml.cs
using System.Windows;
namespace ComboBoxTest
{
/// /// Interaction logic for Window1.xaml
///
public partial class Window1 : Window
{
public Window1()
{
this.DataContext = new ViewModel();
InitializeComponent();
}
}
}ViewModel.cs
using System;
using System.Collections.Generic;
using System.Windows;namespace ComboBoxTest
{public class Colour : IComparable { public string Description { get; set; } #region IComparable Members public int CompareTo(object obj) { return string.Compare(((Colour) obj).Description, this.Description); } #endregion } public class ViewModel : DependencyObject { public List<colour> Colours { get { List<colour> retval = new List<colour>(); retval.Add(new Colour { Description = "Blue" }); retval.Add(new Colour { Description = "Green" }); retval.Add(new Colour { Description = "White" }); retval.Add(new Colour { Description = "Yellow" }); retval.Add(new Colour { Description = "Red" }); retval.Add(new Colour { Description = "Silver" });
Does it work if you implement your Colour class something like this?
public class Colour { public string Description { get; set; } public override bool Equals(object obj) { return this.Description == (obj as Colour).Description; } public override int GetHashCode() { return base.GetHashCode() ^ this.Description.GetHashCode(); } }
...and your SelectedColour dependency property:
public static DependencyProperty SelectedColourProperty = DependencyProperty.Register("SelectedColour", typeof(Colour), typeof(ViewModel)); public Colour SelectedColour { get { return (Colour)GetValue(SelectedColourProperty); } set { SetValue(SelectedColourProperty, value); } }
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Does it work if you implement your Colour class something like this?
public class Colour { public string Description { get; set; } public override bool Equals(object obj) { return this.Description == (obj as Colour).Description; } public override int GetHashCode() { return base.GetHashCode() ^ this.Description.GetHashCode(); } }
...and your SelectedColour dependency property:
public static DependencyProperty SelectedColourProperty = DependencyProperty.Register("SelectedColour", typeof(Colour), typeof(ViewModel)); public Colour SelectedColour { get { return (Colour)GetValue(SelectedColourProperty); } set { SetValue(SelectedColourProperty, value); } }
Mark Salsbery Microsoft MVP - Visual C++ :java:
Many thanks for your reply. That still does not do what i want, i am starting to think that i am going about this the wrong way :( Put simply what i need is a combobox with a datasource of list of my objects (all the same type). Then when the form loads i want the combobox to have preselected an object of my choice (what ever was saved in xml/db) I am new to wpf and the mvvm pattern, and this is getting a bit frustrating!! ps for info i changed the colour class to the following as the other throw an exception
public class Colour { public string Description { get; set; } public override bool Equals(object obj) { if(obj.GetType()== typeof(Colour)) return this.Description == ((Colour) obj).Description; return base.Equals(obj); } public override int GetHashCode() { return base.GetHashCode() ^ this.Description.GetHashCode(); } }