Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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
C# 组合框绑定到多个DataContext_C#_Wpf_Data Binding - Fatal编程技术网

C# 组合框绑定到多个DataContext

C# 组合框绑定到多个DataContext,c#,wpf,data-binding,C#,Wpf,Data Binding,我的应用程序中有一个combobox控件。我使用它从数据库中定义的状态列表中选择一个状态。所选的值应用于为用户类的状态字段(即a)赋值(我有一个表维护数据库中的用户数据) XAML: 代码隐藏: class Register { public User newUser; public ObservableCollection<string> States { get; set; } public Register() { newUser = new Use

我的应用程序中有一个combobox控件。我使用它从数据库中定义的状态列表中选择一个状态。所选的值应用于为用户类的状态字段(即a)赋值(我有一个表维护数据库中的用户数据)

XAML:


代码隐藏:

class Register
{
  public User newUser;
  public ObservableCollection<string> States { get; set; }
  public Register()
  {
    newUser = new User();
    States = new ObservableCollection<string> { "CH", "MA", "KL", "FL" };
    stckDetails.DataContext = newUser;
  }
}


public class User
{
    public int UserId { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string State {get; set; }
 }
类寄存器
{
公共用户newUser;
公共可观测集合状态{get;set;}
公众登记册()
{
newUser=newUser();
状态=新的可观测集合{“CH”、“MA”、“KL”、“FL”};
stckDetails.DataContext=newUser;
}
}
公共类用户
{
public int UserId{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串状态{get;set;}
}

如何将combobox的不同属性绑定到不同的DataContext。可能吗?我使用的语法不起作用。

我做了一些更改。请参阅下面的代码

<Window x:Class="DataContext_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataContext_Learning"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel x:Name="stckDetails">
        <TextBox Margin="10,5,10,5" Text="{Binding NewUser.FirstName, Mode=TwoWay}"/>
        <TextBox Margin="10,5,10,5" Text="{Binding NewUser.LastName, Mode=TwoWay}"/>
        <ComboBox Margin="10,5,10,5" ItemsSource = "{Binding States}" Text="{Binding State, Mode=TwoWay}"/>
    </StackPanel>
</Grid>

名称空间数据上下文\u学习
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.DataContext=新寄存器();
}
}
类寄存器
{
公共用户NewUser{get;set;}
公共可观测集合状态{get;set;}
公众登记册()
{
NewUser=NewUser();
状态=新的可观测集合{“CH”、“MA”、“KL”、“FL”};
}
}
公共类用户
{
public int UserId{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串状态{get;set;}
}
}

这真的很有帮助。
<Window x:Class="DataContext_Learning.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataContext_Learning"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel x:Name="stckDetails">
        <TextBox Margin="10,5,10,5" Text="{Binding NewUser.FirstName, Mode=TwoWay}"/>
        <TextBox Margin="10,5,10,5" Text="{Binding NewUser.LastName, Mode=TwoWay}"/>
        <ComboBox Margin="10,5,10,5" ItemsSource = "{Binding States}" Text="{Binding State, Mode=TwoWay}"/>
    </StackPanel>
</Grid>
    namespace DataContext_Learning
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new Register();
        }
    }

    class Register
    {
        public User NewUser { get; set; }
        public ObservableCollection<string> States { get; set; }
        public Register()
        {
            NewUser = new User();
            States = new ObservableCollection<string> { "CH", "MA", "KL", "FL" };
        }
    }


    public class User
    {
        public int UserId { get; set; }

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string State { get; set; }
    }
}