ComboBox wpf 未选择项目

发布于 2024-08-26 07:34:50 字数 454 浏览 6 评论 0原文

我正在尝试将组合框绑定到对象列表,并且效果很好,除了选定的值之外,我还缺少什么吗?

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

基本上,我想将值绑定到国家/地区代码,并将选定的值设置为绑定到 OrderInfoVm.BillingCountry (实现 INotifyPropertyChanged)的国家/地区代码

。最初,当控件加载时,选定的值是空的,但单击时会填充 BillingCountry。选择的值似乎没有改变。我该如何补救?

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 codes 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

行雁书 2024-09-02 07:34:50

我确实同意 Alex 的观点,即使用 SelectedItem 可以提供所需的行为。请参阅下面的代码。它有效并且希望能进一步帮助您:

    <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;
        }
    }
}

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;
        }
    }
}
萌能量女王 2024-09-02 07:34:50

也许您正在尝试实现与此类似的东西: Bound ComboBox

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

走过海棠暮 2024-09-02 07:34:50

尝试将其更改为 SelectedItem 并设置 Mode=TwoWay...

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

编辑:您可能不需要将其更改为 SelectedItem,也许只需设置 TwoWay 就可以了,但这就是我在自己的代码中完成的方式。

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.

旧人九事 2024-09-02 07:34:50

请确保您指定了正确的绑定路径。
尝试在调试模式下启动项目并查看输出窗口以查看是否存在任何绑定错误

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

夢归不見 2024-09-02 07:34:50

试一试;我相信您将 SelectedValuePath 和 SelectedValue 混合在一起:

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

供参考:

ItemsSource = 获取或设置用于生成 ItemsControl (ComboBox) 内容的集合。

SelectedValue = 获取或设置使用SelectedValuePath 获取的SelectedItem 的值。

SelectedValuePath = 获取或设置一个值,该值指示用于从 SelectedItem 获取 SelectedValue 的路径。

DisplayMemberPath = 获取或设置源对象上的值的路径,以用作对象的视觉表示。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文