wpf 如何使用 XAML 在 DataContext 中设置类?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18972103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a class in the DataContext with XAML?
提问by Lai32290
I have a WPF forms and a class Users (content attributes Id, Login and Name), in my class of this forms, I had get a Users object for put this information in the form with DataContextand Binding
我有一个WPF形式和一类用户(内容属性标识,登录名和姓名),在我班上的这个形式,我就得到一个用户对象把这个信息的形式DataContext和Binding
I can put this Users object to my Window.DataContext (this.DataContext = usersObject;)with code behind, but I think if I can make this with XAML, maybe is better
我可以把这个用户对象放在我Window.DataContext (this.DataContext = usersObject;)后面的代码中,但我认为如果我可以用 XAML 做到这一点,也许会更好
I have set a attribute in my class UserForm (public Users usersObject {get; set;})
我在我的类 UserForm ( public Users usersObject {get; set;}) 中设置了一个属性
My form UserForm : Window
我的表格 UserForm : Window
<Window DataContext="{What need I put here?">
<Grid>
<TextBlock Text="Id:"/>
<TextBox Name="Id" Text="{Binding Path=Id}"/>
<TextBlock Text="Login:"/>
<TextBox Name="Login" Text="{Binding Path=Login}"/>
<TextBlock Text="Name:"/>
<TextBox Name="Name" Text="{Binding Path=Name}"/>
</Grid>
</Window>
UserForm.xaml.cs
用户窗体.xaml.cs
public class UserForm : Window
{
public Users userObject { get; set; }
public UserForm(Users user)
{
InitializeComponent();
this.userObject = user;
}
}
My class Users
我的班级用户
public class Users
{
public int Id { get; set; }
public string Login { get; set; }
public string Name { get; set; }
}
How to I do for set userObjectin the itself Window.DataContextfor TextBox's can put it values?
我如何userObject在自身中Window.DataContext为 TextBox 设置它的值?
回答by 123 456 789 0
Remove the DataContext binding since you are not explicitly doing MVVM pattern. No point of doing the binding.
删除 DataContext 绑定,因为您没有明确执行 MVVM 模式。没有必要进行绑定。
In your Window.xaml
在你的 Window.xaml
<Window>
<Grid>
<TextBlock Text="Id:"/>
<TextBox Name="Id" Text="{Binding Path=Id}"/>
<TextBlock Text="Login:"/>
<TextBox Name="Login" Text="{Binding Path=Login}"/>
<TextBlock Text="Name:"/>
<TextBox Name="Name" Text="{Binding Path=Name}"/>
</Grid>
Add this to your Behind the code Window.xaml.cs
将此添加到您的代码背后Window.xaml.cs
public class UserForm : Window
{
public Users userObject { get; set; }
public UserForm(Users user)
{
InitializeComponent();
this.DataContext = user;
}
}
If you would do it in MVVM you would have done something like this
如果你在 MVVM 中这样做,你会做这样的事情
ViewModel.cs
视图模型.cs
public class UserViewModel
{
private Users _model;
public UserViewModel(Users model)
{
_model = model;
}
public int Id { get { return _model.Id; } }
public string Login { get { return _model.Login; } set { _model.Login; } }
public string Name { get { return _model.Name; } set { _model.Name; } }
}
Now the ViewModel can be customize depending on what you need, you can expose the Model, inject it to the constructor or just set the property value if it is exposed. Don't forget to implement INotifyPropertyChanged if you want to propagate any values in the ViewModel back to the user interface.
现在可以根据您的需要自定义 ViewModel,您可以公开模型,将其注入构造函数或仅设置属性值(如果已公开)。如果要将 ViewModel 中的任何值传播回用户界面,请不要忘记实现 INotifyPropertyChanged。
View.xaml.cs
查看.xaml.cs
public class UserForm : Window {
公共类用户窗体:窗口{
public UserForm(Users user)
{
InitializeComponent();
this.DataContext = new UserViewModel(user);
}
View.xaml
查看.xaml
You have two choices, you can explicitly set the DataContext just like what I did behind the code or you can create a public property that returns the UserViewModel. It's just the same
您有两个选择,您可以像我在代码后面所做的那样显式设置 DataContext,或者您可以创建一个返回 UserViewModel 的公共属性。这是一样的
Model.cs
模型.cs
public class Users { //Whatever properties you need }
Now this is a very simplistic example of MVVM pattern. Once you know the basics, you can then integrate some helpful frameworks that implements MVVM for you like Caliburn Microand MVVM Toolkit
现在这是一个非常简单的 MVVM 模式示例。一旦你了解了基础知识,你就可以集成一些有用的框架来为你实现 MVVM,比如Caliburn Micro和MVVM Toolkit
回答by RonakThakkar
Some time back I had written an article on different options of binding XAML control to code behind property. This might help you.
前段时间我写了一篇关于将 XAML 控件绑定到代码隐藏属性的不同选项的文章。这可能对你有帮助。
http://codingseason.blogspot.in/2013/05/data-binding-xaml-control-to-property.html
http://codingseason.blogspot.in/2013/05/data-binding-xaml-control-to-property.html
回答by Aster Veigas
Create an object of UserForm and assigning data context is pretty simple.
创建一个 UserForm 对象并分配数据上下文非常简单。
UserForm userFormView = new UserForm ();
Users dataContextObject = new Users();
userFormView.DataContext = dataContextObject;
userFormView.Show() //you can also use showdialog. Thats up to you
Its better you remove the following code:
最好删除以下代码:
public Users userObject { get; set; }
public UserForm(Users user)
{
InitializeComponent();
this.userObject = user;
}
Its better to separate the view and the viewmodel. Have a look at MVVM and it will make more sense
最好将视图和视图模型分开。看看 MVVM,它会更有意义
回答by Ashish Singh
This is one of the way to assign DataContext directly from xaml.
这是直接从 xaml 分配 DataContext 的方法之一。
Example App.xaml
示例 App.xaml
<Application.Resources>
<local:Users x:Key="Users"/>
</Application.Resources>
Example UserForm.xaml
示例 UserForm.xaml
<Window DataContext="{StaticResource Users}">
<Grid>
<TextBlock Text="Id:"/>
<TextBox Name="Id" Text="{Binding Path=Id}"/>
<TextBlock Text="Login:"/>
<TextBox Name="Login" Text="{Binding Path=Login}"/>
<TextBlock Text="Name:"/>
<TextBox Name="Name" Text="{Binding Path=Name}"/>
</Grid>
</Window>
回答by yihao
Maybe you can define a DependencyProperty to wrap the access to the userObject, then bind it to the DataContext
也许您可以定义一个 DependencyProperty 来包装对 userObject 的访问,然后将其绑定到 DataContext

