开发者

ComboBox wpf not item not being selected

开发者 https://www.devze.com 2022-12-24 15:52 出处:网络
I am trying to bind a combo box to a list of objects, and it works great, besides the selected value, am I missing somethign?

I am trying to bind a combo box to a list of objects, and it works great, besides the selected value, am I missing somethign?

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
          SelectedValuePath="country_code" DisplayMemberPath="country_name" 
          SelectedValue="{Binding OrderInfoVm.BillingCountry}" />

Basically I want to bind value to country cod开发者_运维百科es and set the selected value to the country code bound to OrderInfoVm.BillingCountry (which implements INotifyPropertyChanged)

Initially when the control loads selected value is empty, but on click BillingCountry is populated. Selected value does not seem to change. How can I remedy that?


I do agree with Alex that using SelectedItem gives the desired behaviour. See the code below. It works and will hopefully help you further:

    <Window x:Class="SelectedValueSpike.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>
        <ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}" 
          SelectedValuePath="country_code" DisplayMemberPath="country_name"  
          SelectedItem="{Binding OrderInfoVm.BillingCountry}"
          IsSynchronizedWithCurrentItem="True"
          Name="AllCountriesBox"/>
        <TextBox Text="{Binding ElementName=AllCountriesBox, Path=SelectedValue}"/>
        <Button>
            Change the textbox to "Ca","NL",or "US" and click!
        </Button>
    </StackPanel>
</Window>

    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Windows;

    namespace SelectedValueSpike
    {
        public partial class Window1 : Window
        {
            public OrderInfoVm OrderInfoVm{ get; set;}
            public Window1()
            {
                InitializeComponent();
                OrderInfoVm=new OrderInfoVm();
                OrderInfoVm.AllCountries.Add(new Country("US","US of A"));
                OrderInfoVm.AllCountries.Add(new Country("NL","Netherlands"));
                OrderInfoVm.AllCountries.Add(new Country("Ca","Canada"));
                OrderInfoVm.BillingCountry = OrderInfoVm.AllCountries[1];
                DataContext = this;
            }
        }



public class OrderInfoVm:INotifyPropertyChanged
    {
        public OrderInfoVm()
        {
            AllCountries=new ObservableCollection<Country>();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<Country> _allCountries;
        public ObservableCollection<Country> AllCountries
        {
            get { return _allCountries; }
            set
            {
                _allCountries = value;
                OnPropertyChanged("AllCountries");
            }
        }

        private Country _billingCountry;
        public Country BillingCountry
        {
            get { return _billingCountry; }
            set
            {
                _billingCountry = value;
                OnPropertyChanged("BillingCountry");
            }
        }

        private void OnPropertyChanged(string property)
        {
            if(PropertyChanged!=null)
                PropertyChanged(this,new PropertyChangedEventArgs(property));
        }
    }

    public class Country
    {
        public string country_code { get; set; }
        public string country_name { get; set; }

        public Country(string code, string name)
        {
            country_code = code;
            country_name = name;
        }
    }
}


Maybe you are trying to implement something similar to this: Bound ComboBox


Try changing it to SelectedItem and set Mode=TwoWay...

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
          SelectedValuePath="country_code" DisplayMemberPath="country_name" 
          SelectedItem="{Binding OrderInfoVm.BillingCountry, Mode=TwoWay}" />

Edit: You may not need to change it to SelectedItem, perhaps just setting TwoWay will work, but that is how I've done it in my own code.


Please ensure that you've specified correct binding path. Try starting project in debug mode and look at the output window to see if there are any binding errors


Give this a shot; I believe you have your SelectedValuePath and SelectedValue mixed up:

<ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}"
   SelectedValue="country_code" 
   DisplayMemberPath="country_name" 
   SelectedValuePath="{Binding OrderInfoVm.BillingCountry}" />

For Reference:

ItemsSource = Gets or sets a collection used to generate the content of the ItemsControl (ComboBox).

SelectedValue = Gets or sets the value of the SelectedItem, obtained by using SelectedValuePath.

SelectedValuePath = Gets or sets a value that indicates the path used to get the SelectedValue from the SelectedItem.

DisplayMemberPath = Gets or sets a path to a value on the source object to serve as the visual representation of the object.

0

精彩评论

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