【发布时间】:2020-02-23 02:04:40
【问题描述】:
我正在尝试根据运行存储过程获得的结果绑定标签的内容。这是我的 XAML:
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<Label Content="Date:" FontSize="10" />
<Label x:Name="HomeScreenLabelInfoDate" FontSize="10" FontWeight="DemiBold" FontStyle="Italic" Content="{Binding InfoDate}"/>
<Label Content="-"/>
<Label Content="Author:" FontSize="10"/>
<Label x:Name="HomeScreenLabelInfoAuthor" Margin="3 0" FontSize="10" FontWeight="DemiBold" FontStyle="Italic" Content="{Binding InfoAuthor}"/>
</StackPanel>
这是我的代码隐藏:
public HomeUserControl()
{
InitializeComponent();
try
{
GetPublications();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class NewsData
{
public string InfoTitle { get; set; }
public string InfoMessage { get; set; }
public string InfoDate { get; set; }
public string InfoAuthor { get; set; }
}
public void GetPublications()
{
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
using (var Connect = new SqlConnection(connstr))
{
Connect.Open();
using (var Command = new SqlCommand("[dbo].[spNewsManagementTb_DisplayNews]", Connect))
{
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.Add("@entity_id", SqlDbType.VarChar).Value = LoggedInData.LoggedInstitutionId;
SqlDataReader dr = Command.ExecuteReader();
while (dr.Read())
{
string newsTitle = dr.GetString(0);
string newsMessage = dr.GetString(1);
string newsAuthor = dr.GetString(2);
DateTime newsDate = dr.GetDateTime(3);
string newsDateFormated = newsDate.ToString("dddd, dd MMMM yyyy");
NewsData newsData = new NewsData();
newsData.InfoTitle = newsTitle;
newsData.InfoDate = newsDateFormated;
newsData.InfoAuthor = newsAuthor;
}
dr.Close();
Connect.Close();
}
}
}
每当我运行应用程序时,我的 GUI 上都看不到任何数据。我已经能够通过从查询读取的值中直接分配标签的值来克服这个问题,但我想继续在整个代码中使用数据绑定以保持一致性,但事实证明这个很难实现。
编辑
我想提一下,我是编程初学者,所以我不太擅长理解所有概念,但 INotifyPropertyChanged 属性引发了多个错误。这是包含 Notify 属性后的代码:
public HomeUserControl()
{
InitializeComponent();
try
{
var newsData = GetPublications();
this.DataContext = newsData;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public NewsData : INotifyPropertyChanged
{
private string Title;
private string Message;
private string Date;
private string Author;
public event PropertyChangedEventHandler PropertyChanged;
public NewsData(string infoTitle, string infoMessage, string infoDate, string infoAuthor)
{
this.Author = infoAuthor;
this.Date = infoDate;
this.Message = infoMessage;
this.Title = infoTitle;
}
public string InfotAuthor
{
get { return Author; }
set
{
Author = InfotAuthor;
OnPropertyChanged();
}
}
public string InfotDate
{
get { return Date; }
set
{
Date = InfotDate;
OnPropertyChanged();
}
}
public string InfotMessage
{
get { return Message; }
set
{
Author = InfotMessage;
OnPropertyChanged();
}
}
public string InfotTitle
{
get { return Title; }
set
{
Author = InfotTitle;
OnPropertyChanged();
}
}
protected void OnPropertyChanged(string Author = null, string Message = null, string Date = null, string Title = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Author));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Message));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Date));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(Title));
}
}
public NewsData GetPublications()
{
string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
using (var Connect = new SqlConnection(connstr))
{
Connect.Open();
using (var Command = new SqlCommand("[dbo].[spNewsManagementTb_DisplayNews]", Connect))
{
Command.CommandType = CommandType.StoredProcedure;
Command.Parameters.Add("@entity_id", SqlDbType.VarChar).Value = LoggedInData.LoggedInstitutionId;
SqlDataReader dr = Command.ExecuteReader();
while (dr.Read())
{
string newsTitle = dr.GetString(0);
string newsMessage = dr.GetString(1);
string newsAuthor = dr.GetString(2);
DateTime newsDate = dr.GetDateTime(3);
string newsDateFormated = newsDate.ToString("dddd, dd MMMM yyyy");
NewsData newsData = new NewsData();
newsData.InfoTitle = newsTitle;
newsData.InfoDate = newsDateFormated;
newsData.InfoAuthor = newsAuthor;
}
dr.Close();
Connect.Close();
}
}
return newsData;
}
错误是由 GetPublications() 方法引起的,该方法无法识别 NewsData
【问题讨论】:
-
NewsData需要支持INotifyPropertyChanged。
标签: c# wpf xaml mvvm data-binding