WPF 标签内容数据绑定
我在 WPF4 中有一个标签,并尝试将内容绑定到 c# 类中的值。我创建了一个 ObjectDataProvider 但由于某种原因看不到内容或更新。你能指出我做错了什么吗?
这是 xaml -
<Grid.Resources>
<local:SummaryData x:Key="mySummaryData"/> </Grid.Resources>
<Label Name ="lbCurrentVerValue" Grid.Row="1" Grid.Column="2" Content="{Binding Path=frameworkVersion, Source={StaticResource mySummaryData}}"/>
<TextBox Name ="lbFromVerValue" Grid.Row="2" Grid.Column="2" Text="{Binding Source={StaticResource mySummaryData}, Path=frameworkVersion}"/>
这是 C# 代码 -
namespace DBUpgradeUI
{
public partial class DBUpgReadinessCheck : Window
{
public string userConnStr = String.Empty;
public string userFoldPath = String.Empty;
public SummaryData sd = new SummaryData();
public DBUpgReadinessCheck()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ReadinessCheck(userConnStr, userFoldPath);
}
public void ReadinessCheck(string connectionString, string folderPath)
{
FrmImportUtility frmWork = new FrmImportUtility();
sd.frameworkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ;
frmWork.CurrentlyProcessingVersion(connectionString, folderPath, ref sd.currentVersion, ref sd.finalVersion);
}
}
public class SummaryData
{
public string currentVersion = "Test";
public string finalVersion = "finalVerTest";
public string frameworkVersion = String.Empty;
}
}
I have a label in WPF4 and trying to bind the content to a value from c# class. I have created an ObjectDataProvider but for some reason can't see the content or updates. Can you point me to what I'm doing wrong?
Here is the xaml -
<Grid.Resources>
<local:SummaryData x:Key="mySummaryData"/> </Grid.Resources>
<Label Name ="lbCurrentVerValue" Grid.Row="1" Grid.Column="2" Content="{Binding Path=frameworkVersion, Source={StaticResource mySummaryData}}"/>
<TextBox Name ="lbFromVerValue" Grid.Row="2" Grid.Column="2" Text="{Binding Source={StaticResource mySummaryData}, Path=frameworkVersion}"/>
and here is the c# code-
namespace DBUpgradeUI
{
public partial class DBUpgReadinessCheck : Window
{
public string userConnStr = String.Empty;
public string userFoldPath = String.Empty;
public SummaryData sd = new SummaryData();
public DBUpgReadinessCheck()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ReadinessCheck(userConnStr, userFoldPath);
}
public void ReadinessCheck(string connectionString, string folderPath)
{
FrmImportUtility frmWork = new FrmImportUtility();
sd.frameworkVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); ;
frmWork.CurrentlyProcessingVersion(connectionString, folderPath, ref sd.currentVersion, ref sd.finalVersion);
}
}
public class SummaryData
{
public string currentVersion = "Test";
public string finalVersion = "finalVerTest";
public string frameworkVersion = String.Empty;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在 SummaryData 中实现 INotifyPropertyChanged:
You must implement INotifyPropertyChanged in SummaryData:
在尝试了在没有依赖属性的情况下从类对象绑定属性并且没有运气的方法之后,我完成了以下操作,它的工作方式就像一个魅力!
xaml -
和 c# 代码 -
这很好用。感谢大家的帮助。
After trying ways to bind properties from a class object without having dependency properties and having no luck, I have done the following and it works like a charm!
xaml -
and c# code -
This works great. Thanks everyone for your help.