WPF 简单绑定
我正在尝试将我的控制台应用程序转换为漂亮的 WPF GUI。我对这段代码有点困惑,想知道是否有人可以提供帮助?
在我的 xaml 中,我有这样的内容:
<CheckBox IsChecked="{Binding CL.LoggedIn}"></CheckBox>
尝试将复选框的值绑定到 CL.LoggedIn 的值。 CL是我引用的类库中的ConnectionLibrary.cs类。
在 xaml 页面的后面代码中,我声明 CL 如下:
public ConnectionLibrary CL = new ConnectionLibrary();
在连接库类中,我已将 :INotifyPropertyChanged 添加到类声明中,并添加了以下代码:
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
我已将 LoggedIn 属性更改为现在如下所示:
private bool loggedIn;
public bool LoggedIn {
get { return loggedIn; }
set { loggedIn = value; OnPropertyChanged("LoggedIn"); }
}
但是,它并没有似乎在我的 xaml 中工作?我在输出窗口中没有收到任何绑定错误,但它没有正确反映 LoggedIn 的值。
有什么想法吗?
谢谢!
I'm trying to convert my console app to a nice WPF GUI. Am getting a little stuck on this code and was wondering if someone can help?
In my xaml I have this:
<CheckBox IsChecked="{Binding CL.LoggedIn}"></CheckBox>
to try and bind the value of the checkbox to the value of CL.LoggedIn. CL is my ConnectionLibrary.cs class in a referenced class library.
In the code behind for the xaml page i declare CL as follows :
public ConnectionLibrary CL = new ConnectionLibrary();
In the connection library class I have added :INotifyPropertyChanged to the class declaration and added the following code:
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
I have changed the LoggedIn property to now look like this:
private bool loggedIn;
public bool LoggedIn {
get { return loggedIn; }
set { loggedIn = value; OnPropertyChanged("LoggedIn"); }
}
However, it doesnt seem to work in my xaml? I dont get any binding errors in the output window, but it doesnt reflect the value of LoggedIn correctly.
Any ideas?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否设置了视图的数据上下文?
在 XAML 文件的代码隐藏中,您需要执行以下操作:
那么绑定是:
绑定将在 DataContext 中的对象上查找命名路径(即 LoggedIn)。
编辑:默认绑定是单向的,这意味着它只能从您的 ViewModel 进行更新。
对于可以输入数据的控件(即:TextBox、CheckBox...),您可以将Binding 设置为“TwoWay”。 Binding 表达式变为:
现在,每当 UI 中的 Checked 状态发生变化时,它都会反映在您的 ViewModel 中。
Have you set the datacontext of your view?
In the code-behind of your XAML file, you need to do:
then the binding is:
The binding will find the the named path (i.e. LoggedIn) on the object that is in the DataContext.
EDIT: The default binding is one-way, this means it only gets updated from your ViewModel.
For controls that can be inputed data (i.e: TextBox, CheckBox...) you can set the Binding as "TwoWay". The Binding expression becomes:
Now whenever the Checked state changes in the UI, it is reflected in your ViewModel.
当您像这样使用
Binding
时,它会绑定到当前的DataContext
,而不是页面本身。解决此问题的最简单方法是在页面构造函数的末尾设置
DataContext = this
。修复此问题的正确方法是使用 MVVM。这意味着将
ConnectionLibrary
放在另一个类的属性中,并将DataContext
设置为另一个类。When you use
Binding
like this, it binds to the currentDataContext
, not to the page itself.The easiest way to fix this would be to set
DataContext = this
at the end of the constructor of the page.The proper way to fix it would be to use MVVM. That would mean having
ConnectionLibrary
in a property of another class and set theDataContext
to this other class.