gpt4 book ai didi

c# - 如何将标签内容与查询中的值绑定(bind)?

转载 作者:行者123 更新时间:2023-12-03 10:31:36 25 4
gpt4 key购买 nike

我正在尝试根据运行存储过程获得的结果绑定(bind)标签的内容。这是我的 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 上都看不到任何数据。我已经能够通过从查询中读取的值直接分配标签的值来克服这个问题,但我想继续在整个代码中使用数据绑定(bind)以保持一致性,但事实证明这个很难实现。

编辑

我想提一下,我是编程初学者,所以我不太擅长理解所有概念,但 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

最佳答案

对您的代码进行以下更改。

  • 使 GetPublications() 返回“NewsData”类型,如下所示,
    public NewsData GetPublications()
    {
    string connstr = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
    NewsData newsData = new NewsData();

    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.InfoTitle = newsTitle;
    newsData.InfoDate = newsDateFormated;
    newsData.InfoAuthor = newsAuthor;

    }

    dr.Close();
    Connect.Close();
    }
    }

    return newsData;
    }
  • 在用户控件的代码隐藏(构造函数)中进行以下更改,
    public HomeUserControl()
    {
    InitializeComponent();

    try
    {
    var newsData = GetPublications();
    this.DataContext = newsData;
    }
    catch (Exception ex)
    {

    MessageBox.Show(ex.Message);
    }
    }
  • 为 NewsData 类实现 INotifyPropertyChanged。请参阅下面的链接了解如何实现此

  • https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification

    小建议:- 我更喜欢以异步方式调用我的数据库,以便我的用户控件加载并等待数据的到来和绑定(bind)。

    试一试,如果您还有任何问题,请告诉我们。

    编辑:- - 更新问题后添加了额外的代码。

    你好,
    我从您的代码中看到了一些错误。
  • 在您的 .xaml 中,您使用“InfoDate”和“InfoAuthor”绑定(bind)到您的标签。
    但是您的 NewsData 没有这些属性。

  • 您的 NewsData 属性名称是“InfotAuthor”和“InfotDate” - 里面有“t”
  • 我已经使用了您的 xaml 代码(在进行了上述更改之后)。

  • 如果没有 INotifyPropertyChanged 继承,我可以看到数据。我想如果您进行上述更改,那么您应该能够在 View 中看到数据。
  • 如果你想实现 INotifyPropertyChanged,你可以按照下面的方法,

    公共(public)类 NewsData : INotifyPropertyChanged
    {
    私有(private)字符串_title;
    private string _message;

    private string _date;

    private string _author;

    public string InfoAuthor
    {
    get { return _author; }

    set
    {
    _author = value;
    OnPropertyChanged("InfoAuthor");
    }
    }

    public string InfoDate
    {
    get { return _date; }

    set
    {
    _date = value;
    OnPropertyChanged("InfoDate");
    }
    }

    public string InfoMessage
    {
    get { return _message; }

    set
    {
    _message = value;
    OnPropertyChanged("InfoMessage");
    }
    }

    public string InfoTitle
    {
    get { return _title; }

    set
    {
    _title = value;
    OnPropertyChanged("InfoTitle");
    }
    }

    protected void OnPropertyChanged(string propertyName = null)
    {
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;

    }

  • I have made private members to start with "_" with camel casing (this is just correct naming conventions for back-end fields)

    I have implemented OnPropertyChanged() method and I am calling from each property setter method.

    In your setter method, you are trying to set like below which is wrong,


    Date = InfotDate;

    您需要设置为 “日期=值;”

    希望您能从我更新的答案中理解差异。

    试一试,如果您还有其他问题,请告诉我们。

    如果它回答了您的问题,请接受它作为答案。

    关于c# - 如何将标签内容与查询中的值绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60358592/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com