【问题标题】:ComboBox wpf not item not being selectedComboBox wpf不是未选择的项目
【发布时间】:2010-03-20 22:54:18
【问题描述】:

我正在尝试将组合框绑定到对象列表,并且效果很好,除了选定的值之外,我是否缺少某些东西?

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

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

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

【问题讨论】:

  • `SelectedValue="{Binding OrderInfoVm.BillingCountry.country_code}" 在我在答案中给出的代码中也工作得更好。但是,在这种情况下,文本框不会与组合框同步。使用 SelectedItem!

标签: wpf binding


【解决方案1】:

我同意 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;
        }
    }
}

【讨论】:

  • 非常感谢,这就是发生的事情,我正在绑定到从数据库填充的自定义类。无论我尝试什么,它都不会选择。在我将内容复制到字典后,一切都开始工作了。我不太确定为什么会这样。我认为这是因为选择的值是一个字符串,而对象的值是 SqlString 类型的。会这样吗?
  • 这很有可能。你应该有一个数据绑定错误。您知道如何在您的输出窗口中获得详细的调试报告吗? bea.stollnitz.com/blog/?p=52
【解决方案2】:

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

【讨论】:

  • 谢谢,不完全是,我的只是绑定到数据库对象,但这有点帮助:)
【解决方案3】:

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

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

编辑:您可能不需要将其更改为 SelectedItem,也许只需设置 TwoWay 即可,但我在自己的代码中就是这样做的。

【讨论】:

  • 由于某种原因,两者都不起作用。另外,如果我将 selectedvalue 更改为“US”,它仍然不起作用:(我不明白
  • SelectedItem 不能与 SelectedValuePath 一起使用,因为它会选择对象本身。但如果需要值,则应使用 SelectedValue。
【解决方案4】:

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

【讨论】:

    【解决方案5】:

    试一试;我相信您的 SelectedValuePath 和 SelectedValue 混淆了:

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

    供参考:

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

    SelectedValue = 获取或设置SelectedItem的值,通过SelectedValuePath获取。

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

    DisplayMemberPath = 获取或设置源对象上的值的路径,作为对象的可视化表示。

    【讨论】:

    • 不,实际上这在我的情况下是不正确的,应该交换 selectedvaluepath 和 selectedvalue,我两种方式都测试过
    猜你喜欢
    • 2016-01-17
    • 2023-03-12
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多