是否可以将数据放入 UI 元素中?
Is it possible to put data in an UI element?
比如我有这个class
public class Task : DependencyObject
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string TaskName { get; set; }
public string Description { get; set; }
public DateTime BeginDate { get; set; }
public DateTime? Duration { get; set; }
public bool Done { get; set; }
}
我想将它保存在 CheckBox 实例上。是否可以将其完整保存,或者至少保存一个可以放置 int 的属性?
我需要这个,因为选中复选框时,我需要知道选中了哪个元素。
我如何生成 UI :
private void pageLoaded(object sender, RoutedEventArgs e)
{
var obj = App.Current as App;
var query = obj.Connection.Table<Task>();
foreach (var task in query)
{
var stackLine = new StackPanel();
stackLine.Orientation = Orientation.Horizontal;
TextBlock taskName = new TextBlock();
taskName.Text = task.TaskName;
TextBlock taskDate = new TextBlock();
taskDate.Text = " " + task.BeginDate.ToString();
CheckBox taskDone = new CheckBox();
taskDone.Checked += new RoutedEventHandler(taskChecked);
if (task.Done == true)
taskDone.IsChecked = true;
stackLine.Children.Add(taskDone);
stackLine.Children.Add(taskName);
stackLine.Children.Add(taskDate);
listView.Items.Add(stackLine);
}
}
您不应该浪费时间在 C# 中生成 UI;你应该把它写在 XAML 中并写一个视图模型。你做事很艰难。从长远来看,我建议您学习 so-called "right way" 以与 XAML 一起工作。它可以让你轻松地做很多很酷的事情,也可以让你做一些令人惊奇的事情 moderate-to-unbearable 难度。
但是您今天可能不会重写整个应用程序,您可以很容易地拼凑出您已有的内容,如下所示。
创建 CheckBox
时,将其 Tag
属性 设置为属于它的 Task
对象:
CheckBox taskDone = new CheckBox();
taskDone.Checked += new RoutedEventHandler(taskChecked);
// Here:
taskDone.Tag = task;
在taskChecked
方法中:
private void taskChecked(object sender, RoutedEventArgs e)
{
var cb = (CheckBox)sender;
// Here we get the Task back from the Tag property.
// Tag is type Object, so we need to cast this one too.
var task = (Task)cb.Tag;
// Do stuff with the Task
task.Done = cb.IsChecked;
}
比如我有这个class
public class Task : DependencyObject
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string TaskName { get; set; }
public string Description { get; set; }
public DateTime BeginDate { get; set; }
public DateTime? Duration { get; set; }
public bool Done { get; set; }
}
我想将它保存在 CheckBox 实例上。是否可以将其完整保存,或者至少保存一个可以放置 int 的属性?
我需要这个,因为选中复选框时,我需要知道选中了哪个元素。
我如何生成 UI :
private void pageLoaded(object sender, RoutedEventArgs e)
{
var obj = App.Current as App;
var query = obj.Connection.Table<Task>();
foreach (var task in query)
{
var stackLine = new StackPanel();
stackLine.Orientation = Orientation.Horizontal;
TextBlock taskName = new TextBlock();
taskName.Text = task.TaskName;
TextBlock taskDate = new TextBlock();
taskDate.Text = " " + task.BeginDate.ToString();
CheckBox taskDone = new CheckBox();
taskDone.Checked += new RoutedEventHandler(taskChecked);
if (task.Done == true)
taskDone.IsChecked = true;
stackLine.Children.Add(taskDone);
stackLine.Children.Add(taskName);
stackLine.Children.Add(taskDate);
listView.Items.Add(stackLine);
}
}
您不应该浪费时间在 C# 中生成 UI;你应该把它写在 XAML 中并写一个视图模型。你做事很艰难。从长远来看,我建议您学习 so-called "right way" 以与 XAML 一起工作。它可以让你轻松地做很多很酷的事情,也可以让你做一些令人惊奇的事情 moderate-to-unbearable 难度。
但是您今天可能不会重写整个应用程序,您可以很容易地拼凑出您已有的内容,如下所示。
创建 CheckBox
时,将其 Tag
属性 设置为属于它的 Task
对象:
CheckBox taskDone = new CheckBox();
taskDone.Checked += new RoutedEventHandler(taskChecked);
// Here:
taskDone.Tag = task;
在taskChecked
方法中:
private void taskChecked(object sender, RoutedEventArgs e)
{
var cb = (CheckBox)sender;
// Here we get the Task back from the Tag property.
// Tag is type Object, so we need to cast this one too.
var task = (Task)cb.Tag;
// Do stuff with the Task
task.Done = cb.IsChecked;
}