开发者

Bind A ComboBox to a static Lookup table and bind the current Item to the user control class lookupID

I have a Static Class that supply me lookup tables (ObservableCollectoins) of a List. (List of Cities for example)

public static GeneralData
{
    public static ObservableCollection开发者_StackOverflow中文版<City> colCity;
}

I have a UserControl with a combobox. it's DataContext is another class (Person)

Person has a CityID

I want to bind the ComboBox to the Cities table and have it set the CurrentItem to the Person.CityID

When someone change the city (in the combo box) I want the Person CityID to be changed.

It is possible ?

(it looks like the problem is either to bind the combo box to the static class or to the person, but not both...)

This Code give me the combobox:

cbxCity.ItemsSource = GeneralData.colCity;

and This is the XAML - But how can I bind it to the Person CityID ?

<ComboBox Name="cbxCity"  DisplayMemberPath="CityName" SelectedValuePath="CityID" SelectedItem="{Binding Path=CityID}" Width="80"></ComboBox>


This is certainly possible and could be done a few ways. The solution below uses an IValueConverter. Also note that I have overridden Equals and GetHashCode within the City class since the static class returning the listing of cities is creating a new instance each time.

The code behind is as follows and serves as an example and is in no way thoroughly tested; however it meets your needs...

    public partial class Window1 : Window
    {
        private Data _data = new Data();

        public Window1()
        {
            InitializeComponent();

            this.DataContext = _data;
        }
    }

    public class Data
    {
        private Person _person = new Person() { CityId = 3 };
        public Person Person
        {
            get
            {
                return _person;
            }
        }

        public ObservableCollection<City> Citys
        {
            get
            {
                return GeneralData.Citys;
            }
        }
    }

    public class Person : INotifyPropertyChanged
    {
        private int _cityId = -1;
        public int CityId
        {
            get
            {
                return _cityId;
            }
            set
            {
                _cityId = value;
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("CityId"));

                Console.WriteLine("My new CityId is: " + _cityId);
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class City : INotifyPropertyChanged
    {
        private String _cityName = "N/A";
        public String CityName
        {
            get
            {
                return _cityName;
            }
            set
            {
                _cityName = value;
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("CityName"));
            }
        }

        private int _cityId = -1;
        public int CityId
        {
            get
            {
                return _cityId;
            }
            set
            {
                _cityId = value;
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs("CityId"));
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            City c = obj as City;
            if (c == null)
            {
                return false;
            }

            return this.CityId == c.CityId && String.Compare(this.CityName, c.CityName, true) == 0;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode() ^ CityId;
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public static class GeneralData
    {
        public static ObservableCollection<City> Citys
        {
            get
            {
                ObservableCollection<City> citites = new ObservableCollection<City>{
                    new City { CityId = 1, CityName = "Denver" },
                    new City { CityId = 2, CityName = "Phoenix" },
                    new City { CityId = 3, CityName = "San Diego" },
                    new City { CityId = 4, CityName = "Pasadena" },
                    new City { CityId = 5, CityName = "Sedona" }};

                return citites;
            }
        }
    }

    public class CityConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int cityId = (int)value;
            return GeneralData.Citys.Single(i => i.CityId == cityId);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            City c = (City)value;
            return c.CityId;
        }

        #endregion
    }

The XAML is short and simple...

<Window.Resources>
        <local:CityConverter x:Key="CityConverter"/>
    </Window.Resources>

    <Grid>
        <ComboBox ItemsSource="{Binding Citys}"
                  DisplayMemberPath="CityName"
                  SelectedItem="{Binding Person.CityId, Converter={StaticResource CityConverter}}">
        </ComboBox>
    </Grid>


If you change SelectedItem in your XAMl to SelectedValue does it work?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜