Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
组合框wpf未选中项目_Wpf_Binding - Fatal编程技术网

组合框wpf未选中项目

组合框wpf未选中项目,wpf,binding,Wpf,Binding,我正在尝试将一个组合框绑定到一个对象列表,它工作得很好,除了所选的值之外,我是否缺少一些东西 <ComboBox ItemsSource="{Binding OrderInfoVm.AllCountries}" SelectedValuePath="country_code" DisplayMemberPath="country_name" SelectedValue="{Binding OrderInfoVm.BillingCountry}" /

我正在尝试将一个组合框绑定到一个对象列表,它工作得很好,除了所选的值之外,我是否缺少一些东西

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

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


最初,当控件加载选定值时为空,但单击BillingCountry时填充。所选值似乎没有更改。我怎样才能补救呢?

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

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


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

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

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


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

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

请确保指定了正确的绑定路径。
尝试在调试模式下启动project,并查看输出窗口以查看是否存在任何绑定错误;我相信您的SelectedValuePath和SelectedValuePath混淆了:

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

供参考:

=获取或设置用于生成ItemsControl(组合框)内容的集合

=获取或设置SelectedValuePath获取的SelectedItem的值

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


=获取或设置源对象上的值的路径,以用作该对象的可视表示形式

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

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

供参考:

=获取或设置用于生成ItemsControl(组合框)内容的集合

=获取或设置SelectedValuePath获取的SelectedItem的值

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


=获取或设置源对象上的值的路径,以用作该对象的可视表示形式

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

将文本框更改为“Ca”、“NL”或“US”,然后单击!
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows;
命名空间SelectedValueSpike
{
公共部分类Window1:Window
{
public OrderInfoVm OrderInfoVm{get;set;}
公共窗口1()
{
初始化组件();
OrderInfoVm=新的OrderInfoVm();
OrderInfoVm.AllCountries.Add(新国家(“美国”、“美国的”);
OrderInfoVm.AllCountries.Add(新国家(“荷兰”);
OrderInfoVm.AllCountries.Add(新国家(“Ca”、“加拿大”);
OrderInfoVm.BillingCountry=OrderInfoVm.AllCountries[1];
DataContext=this;
}
}
公共类OrderInfoVm:INotifyPropertyChanged
{
PublicOrderInfoVM()
{
所有国家/地区=新的可观测集合();
}
公共事件属性更改事件处理程序属性更改;
所有国家的私人可观测收集;
所有国家/地区的公共可观测集合
{
获取{return\u allCountries;}
设置
{
_所有国家=价值;
不动产变更(“所有国家”);
}
}
私人国家(比林国家),;
公共国家计费国家
{
获取{return\u billingCountry;}
设置
{
_billingCountry=价值;
房地产变更(“BillingCountry”);
}
}
私有void OnPropertyChanged(字符串属性)
{
if(PropertyChanged!=null)
PropertyChanged(此,新PropertyChangedEventArgs(property));
}
}
公营国家
{
公共字符串国家代码{get;set;}
公共字符串country_name{get;set;}
公共国家(字符串代码、字符串名称)
{
国家代码=代码;
国家名称=名称;
}
}
}

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

将文本框更改为“Ca”、“NL”或“US”,然后单击!
使用System.Collections.ObjectModel;
使用系统组件模型;
使用System.Windows;
命名空间SelectedValueSpike
{
公共部分类Window1:Window
{
public OrderInfoVm OrderInfoVm{get;set;}
公共窗口1()
{
初始化组件();
OrderInfoVm=新的OrderInfoVm();
OrderInfoVm.AllCountries.Add(新国家(“美国”、“美国的”);
OrderInfoVm.AllCountries.Add(新国家(“荷兰”);
OrderInfoVm.AllCountries.Add(新国家(“Ca”、“加拿大”);
OrderInfoVm.BillingCountry=OrderInfoVm.AllCountries[1];
DataContext=this;
}
}
公共类OrderInfoVm:INotifyPropertyChanged
{
PublicOrderInfoVM()
{
所有国家/地区=新的可观测集合();
}
公共事件属性更改事件处理程序属性更改;
所有国家的私人可观测收集;
公众可观察的