c# – WPF绑定问题 – UI更新,对象不
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WPF绑定问题 – UI更新,对象不,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3449字,纯文字阅读大概需要5分钟。
内容图文
![c# – WPF绑定问题 – UI更新,对象不](/upload/InfoBanner/zyjiaocheng/813/f538f971b66e4d52a546785efc8cb365.jpg)
我还有另一个WPF绑定问题.就在我认为我发现了这些东西的时候,我遇到了更多问题……:S
无论如何……我已经创建了一个用于选择文件的自定义用户控件.它是一个简单的文本框,后跟一个包含在网格中的按钮.我正在使用的控件的属性称为FilePath,此控件上的TextBox绑定到该属性.单击该按钮时,将打开SaveFileDialog,用户选择一个文件.用户选择文件后,UI会正确更新.
我似乎遇到的问题是,当我将一个对象绑定到控件时(在这个例子中我有一个带有DocumentFilePath属性的对象),当选择一个新文件时,该对象不会更新.
这是我的用户控件中的相关代码:
public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register("FilePath", typeof(string), typeof(FileSave), new UIPropertyMetadata(string.Empty, OnFilePathChanged));
public string FilePath
{
get
{
return this.GetValue(FilePathProperty) as string;
}
set
{
this.SetValue(FilePathProperty, value);
this.OnPropertyChanged("FilePath");
}
}
private void OnPropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
private static void OnFilePathChanged(object sender, DependencyPropertyChangedEventArgs e)
{
((FileSave)sender).OnPropertyChanged("FilePath");
}
并且通过在我的对象上使用反射以编程方式将用户控件添加到我的Window中:
private void AddFileSave(PropertyInfo pi)
{
FileSave fs = new FileSave();
Binding b = new Binding(pi.Name);
fs.SetBinding(FileSave.FilePathProperty, b);
this.AddToGrid(fs); //adds the control into my window's grid in the correct row and column; nothing fancy here
}
值得注意的是,如果我使用现有对象加载窗口,我的用户控件将正确显示,但仍然不会在其绑定的对象中注册任何更改.
如果你们需要更多信息,请告诉我.
提前致谢,
桑尼
编辑:我找到了解决问题的方法,但这可能不是一个好的解决方案.通过仔细观察调试器,我发现当我在控件中设置FilePath属性时,该对象未被绑定.如果有人能够对此有所了解,我将非常感激.与此同时,我更改了打开我的SaveFileDialog的代码,如下所示:
private void Button_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Multiselect = false;
ofd.Title = "Select document to import...";
ofd.ValidateNames = true;
ofd.ShowDialog();
if (this.GetBindingExpression(FilePathProperty) == null)
{
this.FilePath = ofd.FileName;
}
else //set value on bound object (THIS IS THE NEW PORTION I JUST ADDED)
{
BindingExpression be = this.GetBindingExpression(FilePathProperty);
string propName = be.ParentBinding.Path.Path;
object entity = be.DataItem;
System.Reflection.PropertyInfo pi = entity.GetType().GetProperty(propName);
pi.SetValue(entity, ofd.FileName, null);
}
if (!string.IsNullOrWhiteSpace(this.FilePath))
{
_fileContents = new MemoryStream();
using (StreamReader sr = new StreamReader(this.FilePath))
{
_fileContents = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(sr.ReadToEnd()));
}
}
else
{
_fileContents = null;
}
}
解决方法:
您没有在代码中的任何位置指定FilePath属性应该是TwoWay,因此DP值的更新不会被推送到绑定的源对象的属性.你可以使用:
Binding b = new Binding(pi.Name){ Mode = BindingMode.TwoWay };
或者您可以设置您的依赖属性以使用默认值TwoWay:
public static readonly DependencyProperty FilePathProperty = DependencyProperty.Register(
"FilePath", typeof(string), typeof(FileSave),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFilePathChanged));
您还应该遵循Robert关于删除手动PropertyChange事件的建议,也不要在DP包装器属性中添加除GetValue和SetValue之外的任何代码. XAML直接调用GetValue和SetValue,因此会跳过你在那里添加的任何其他东西 – 这可能会导致非常讨厌的错误.
内容总结
以上是互联网集市为您收集整理的c# – WPF绑定问题 – UI更新,对象不全部内容,希望文章能够帮你解决c# – WPF绑定问题 – UI更新,对象不所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。