如何以编程方式绑定属性



这是我的代码:

using MySqlConnector;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
namespace Flanium_Agent
{
public class Agency
{
public string Station { get; set; }
public string Process { get; set; }
public string Actions { get; set; }
public string Started { get; set; }
public string Finished { get; set; }
public string Status { get; set; }
public MySqlConnection connection =new MySqlConnection("Server=localhost;User ID=root;Database=orchestration_db");
private Grid DisplayGrid { get; set; }
private Border border { get; set; }
public Grid getDisplayGrid()
{
return DisplayGrid;
}
public Agency InsertAgentToGrid(Grid grid)
{
DisplayGrid = new Grid();

var headerRowDefinition = new RowDefinition();
headerRowDefinition.Height = new GridLength(50);
DisplayGrid.RowDefinitions.Add(headerRowDefinition);
for (var index = 0; index < GetType().GetProperties().Length; index++)
{
var property = GetType().GetProperties()[index];
var header = new TextBlock();
var column = new ColumnDefinition();
header.Text = property.Name;
header.HorizontalAlignment = HorizontalAlignment.Center;
header.VerticalAlignment = VerticalAlignment.Center;
header.FontSize = 16;
Grid.SetRow(header, 0);
Grid.SetColumn(header, index);
DisplayGrid.ColumnDefinitions.Add(column);
DisplayGrid.Children.Add(header);
}
var contentRowDefinition = new RowDefinition();
contentRowDefinition.Height = new GridLength(50);
DisplayGrid.RowDefinitions.Add(contentRowDefinition);
for (var index = 0; index < GetType().GetProperties().Length; index++)
{
var propertyValue = GetType().GetProperties()[index];
var content = new TextBlock();

//content.Text = propertyValue.GetValue(this).ToString();
content.HorizontalAlignment = HorizontalAlignment.Center;
content.VerticalAlignment = VerticalAlignment.Center;
content.FontSize = 12;
Grid.SetRow(content, 1);
Grid.SetColumn(content, index);
DisplayGrid.Children.Add(content);

var myBinding = new Binding(propertyValue.Name)
{
Source = this
};
content.SetBinding(TextBlock.TextProperty, myBinding);
}
DisplayGrid.HorizontalAlignment = HorizontalAlignment.Stretch;
DisplayGrid.VerticalAlignment = VerticalAlignment.Top;
DisplayGrid.Margin = new Thickness(0, 25, 0, 0);
grid.Children.Add(DisplayGrid);

connection.Open();
//create new thread with UpdateAgent()
Task.Run(UpdateAgent);

return this;
}
public void UpdateAgent()
{
try
{
var cmd = new MySqlCommand("SELECT * FROM agents WHERE station='" + Station + "'", connection);
var reader = cmd.ExecuteReader();

reader.Read();
Station = reader.GetString(0);
Process = reader.GetString(1);
Actions= reader.GetString(2);
Started = reader.GetString(3);
Finished = reader.GetString(4);
Status = reader.GetString(5);
reader.Close();

Thread.Sleep(1000);
Task.Run(UpdateAgent);
}
catch (Exception exception)
{

}
}
public Agency(string station, string process, string actions, string started, string finished, string status)
{
try
{
Station = station;
Process = process;
Actions = actions;
Started = started;
Finished = finished;
Status = status;
connection.Open();
var cmd = new MySqlCommand("INSERT INTO agents (station, process, actions, started, finished, status) VALUES (@station, @process, @actions, @started, @finished, @status)", connection);
cmd.Parameters.AddWithValue("@station", station);
cmd.Parameters.AddWithValue("@process", process);
cmd.Parameters.AddWithValue("@actions", actions);
cmd.Parameters.AddWithValue("@started", started);
cmd.Parameters.AddWithValue("@finished", finished);
cmd.Parameters.AddWithValue("@status", status);
cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception e)
{
}
}
public void RemoveAgent()
{
try
{
connection.Close();
connection.Open();
using var cmd = new MySqlCommand("DELETE FROM agents WHERE station = @station", connection);
cmd.Parameters.AddWithValue("@station", Station);

cmd.ExecuteNonQuery();
connection.Close();
}
catch (Exception e)
{
connection.Close();

}

}
}
}

我可以从以下几行确认绑定设置正确:

var myBinding = new Binding(propertyValue.Name)
{
Source = this
};
content.SetBinding(TextBlock.TextProperty, myBinding);

因为即使我没有将text属性设置为这样的值:content。Text=propertyValue.GetValue(this(.ToString((;因为它已被注释,所以文本值仍在更新中。

然而。。我有一个递归方法,每1000ms运行一次,名为UpdateAgent((,这个方法从mySQL数据库中读取并更新类,现在我不明白为什么即使类属性已经更新,内容TextBlock的这个文本属性也不会更新,即使已经设置了绑定。

我已经浏览并查找了重复项,但找不到任何相关的内容,也找不到试图使用递归方法从SQL数据库中获取数据的人,以及他们的绑定不更新等。

WPF无法知道属性发生了更改,因此Binding已过期。

您应该在Agency类上实现INotifyPropertyChanged,并在任何属性更改时广播事件。作为优化,如果你知道你要同时更改所有属性,你可以引发一个事件(而不是每个属性一个(。

public class Agency : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void UpdateAgent()
{
// ... elided for clarity
// set all properties
Station = reader.GetString(0);
Process = reader.GetString(1);
Actions= reader.GetString(2);
Started = reader.GetString(3);
Finished = reader.GetString(4);
Status = reader.GetString(5);
// ** ADD THIS CODE **
// raise the PropertyChanged event so WPF updates bindings
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(""));
// ... elided for clarity
}
}

最新更新