Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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_Mvvm - Fatal编程技术网

WPF组合框获取和设置数据

WPF组合框获取和设置数据,wpf,mvvm,Wpf,Mvvm,我将WPF与MVVM一起使用。问题是我有一个模型类说Person包含三个属性PersonID、Name、Job。视图模型包含Personclass。视图包含作业的组合框,我使用组合框项比如工程师、律师、,医生:现在我需要绑定到Person.Job属性的组合框,以便在视图显示特定人员时显示人员的作业,并能够输入/更改作业和要提交给人员的新值(即,我希望使用类似MS Access表单中的组合框) 提前谢谢。 这里是组合框的XAML代码 <ComboBox ItemsSource="{Bindi

我将WPF与MVVM一起使用。问题是我有一个模型类说Person包含三个属性PersonID、Name、Job。视图模型包含
Person
class。视图包含
作业的
组合框
,我使用
组合框项
比如工程师、律师、,医生:现在我需要绑定到
Person.Job
属性的组合框,以便在视图显示特定人员时显示人员的作业,并能够输入/更改作业和要提交给人员的新值(即,我希望使用类似MS Access表单中的组合框) 提前谢谢。 这里是
组合框的XAML代码

<ComboBox ItemsSource="{Binding Person, UpdateSourceTrigger=PropertyChanged}" 
   SelectedValue="{Binding Path=Job, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
   SelectedValuePath="PersonID" 
   SelectedItem="{Binding Person.Job, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
   Width="50" Height="20" 
   Grid.Row="1" Grid.Column="2" 
   HorizontalAlignment="Left">
   <ComboBoxItem Content="Engineer"/>
   <ComboBoxItem Content="Lawyer"/>
   <ComboBoxItem Content="Doctor"/>
</ComboBox>

作业在组合框中正确显示,但在保存新的或修改的值时,我会在数据库中找到“System.Windows.Controls.ComboxItem:Engineer”如何更正此问题。
谢谢

当通知人员更改时,通知组合框上的绑定更改


PS用代码回答会更容易。

当通知人员更改时,通知组合框上绑定的更改


PS用代码回答会更容易。

假设
Person.Job
是一个
字符串,没有ID,并且您希望保持您的工作列表静态,请尝试如下设置您的
组合框

<ComboBox SelectedItem="{Binding Path=Job, Mode=TwoWay}">
   <ComboBox.ItemsSource>
       <x:Array Type="sys:String" xmlns:sys="clr-namespace:System;assembly=mscorlib">
           <sys:String>Engineer</sys:String>
           <sys:String>Lawyer</sys:String>
           <sys:String>Doctor</sys:String>
       </x:Array>
   </ComboBox.ItemsSource>
</ComboBox>

工程师
律师
医生

在您的案例中,问题是您的
作业
是一个
字符串
,您将
组合框
项定义为
组合框项
,这意味着
SelectedItem
将属于
组合框项
类型,当您选择一个WPF时,将执行转换,在本例中,这意味着调用
ToString()
,这就是你的数据。在上面的示例中,您将
ItemsSource
设置为字符串数组,该数组由WPF在
ComboBoxItem
中包装以供演示,但是
SelectedItem
将是
string
类型

,假设
Person.Job
字符串
,没有ID,并且您希望保持工作列表为静态,请尝试如下设置您的
组合框

<ComboBox SelectedItem="{Binding Path=Job, Mode=TwoWay}">
   <ComboBox.ItemsSource>
       <x:Array Type="sys:String" xmlns:sys="clr-namespace:System;assembly=mscorlib">
           <sys:String>Engineer</sys:String>
           <sys:String>Lawyer</sys:String>
           <sys:String>Doctor</sys:String>
       </x:Array>
   </ComboBox.ItemsSource>
</ComboBox>

工程师
律师
医生

在您的案例中,问题是您的
作业
是一个
字符串
,您将
组合框
项定义为
组合框项
,这意味着
SelectedItem
将属于
组合框项
类型,当您选择一个WPF时,将执行转换,在本例中,这意味着调用
ToString()
,这就是你的数据。在上面的示例中,您将
ItemsSource
设置为字符串数组,该数组由WPF在
ComboBoxItem
中包装以供显示,但是
SelectedItem
将是
string
类型

您可以通过此方法轻松处理静态绑定

Xaml代码

<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"  SelectedItem="{Binding PaymentType}"  Width="150" Grid.Column="1" Margin="50,4,0,0" Grid.Row="9">
                <ComboBox.Items>
                    <ComboBoxItem Content="Cash Payment"  />
                    <ComboBoxItem Content="Cheque Payment"  />                
                </ComboBox.Items>
 </ComboBox>
    private System.Windows.Controls.ComboBoxItem _PaymentType;
    public System.Windows.Controls.ComboBoxItem PaymentType
    {
              get
              {
                  return _PaymentType;
              }
              set
              {
                  _PaymentType = value;
                  RaisedPropertyChanged("PaymentType");
              }
    }
现在您可以使用变量PaymentType作为

string output = PaymentType.Content.ToString();

您可以通过此方法轻松地处理静态绑定

Xaml代码

<ComboBox HorizontalAlignment="Left" VerticalAlignment="Top"  SelectedItem="{Binding PaymentType}"  Width="150" Grid.Column="1" Margin="50,4,0,0" Grid.Row="9">
                <ComboBox.Items>
                    <ComboBoxItem Content="Cash Payment"  />
                    <ComboBoxItem Content="Cheque Payment"  />                
                </ComboBox.Items>
 </ComboBox>
    private System.Windows.Controls.ComboBoxItem _PaymentType;
    public System.Windows.Controls.ComboBoxItem PaymentType
    {
              get
              {
                  return _PaymentType;
              }
              set
              {
                  _PaymentType = value;
                  RaisedPropertyChanged("PaymentType");
              }
    }
现在您可以使用变量PaymentType作为

string output = PaymentType.Content.ToString();

:另一个帮助:如何使用SelectedItem对这样的组合框进行验证,我尝试过,但验证不起作用。你想实现什么?看看我在模型类(Person)中使用IDataErrorInfo进行验证,上面的组合框替换了验证正常的文本框。但对于通过SelectedItem进行绑定的combobox,它不起作用,尽管我使用了ValidateSondaErrors=True、ValidatesOnExceptions=True、NotifyOnValidationError=True,但我不理解“combobox替换的文本框”。它过去是,现在仍然是
组合框
,我在您的XAML中没有看到任何验证代码。您要验证人员的哪一部分?在该组合框上,您只能验证
Job
,如果您的
Person
类实现了
IDataErrorInfo
并且
Person[“Job”]
返回一个错误,如果无效,则
SelectedItem=“{Binding Path=Job,Mode=TwoWay,validatesondaerrors=True}”
应该可以解决您的问题:还有一个帮助:如何使用SelectedItem对这样的组合框进行验证,我尝试过,但验证不起作用。您想实现什么?看看我在模型类(Person)中使用IDataErrorInfo进行验证,上面的组合框替换了验证正常的文本框。但对于通过SelectedItem进行绑定的combobox,它不起作用,尽管我使用了ValidateSondaErrors=True、ValidatesOnExceptions=True、NotifyOnValidationError=True,但我不理解“combobox替换的文本框”。它过去是,现在仍然是
组合框
,我在您的XAML中没有看到任何验证代码。您要验证人员的哪一部分?在该组合框上,您只能验证
Job
,如果您的
Person
类实现了
IDataErrorInfo
并且
Person[“Job”]
返回一个错误,如果无效,则
SelectedItem=“{Binding Path=Job,Mode=TwoWay,validatesondarorrs=True}”应该可以解决您的问题