DialogBox 中的空标签内容 (WPF)

Null Label Content in DialogBox (WPF)

我是 WPF 的初学者。我有一个带有主 window 和对话框的应用程序。当我加载对话框时,我在对话框的标签内容中放置了一个数字(从数据库中获取)。一切顺利,我可以在对话框的标签中看到获取的数字,但是当我尝试使用它在我的对话框中获取一些数据时,WPF 说标签内容为空。 我把一个数字作为idNum的内容。它工作正常。所以我认为我的代码没有问题,但我不明白为什么它在 dlg.lbId.

中显示为“null”

在我的主要 window 后面的代码中,我有:

    private void btManage_Click(object sender, RoutedEventArgs e)
    {
        var selected = lvOwnerCar.SelectedItem as Owner;
        int selectedId = selected.Id;
        string selectedName = selected.Name;
        CarsDialog dlg = new CarsDialog();
        dlg.lbName.Content = selectedName;
        dlg.lbId.Content = selectedId;
        dlg.Owner = this;
        dlg.ShowDialog();
    }

在对话框后面的代码中我有:

        public CarsDialog()
    {
        InitializeComponent();
        ctx = new CarDbContext();
        lvCars.ItemsSource = (from c in ctx.Cars select c).ToList<Car>();
        int idNum = Convert.ToInt32(lbId.Content);
        lvCars.ItemsSource = (from c in ctx.Cars where c.Owner.Id == idNum select c).ToList<Car>();

    }

问题可能是您试图在 lbId.Content 真正设置之前阅读它。尝试将加载数据从 CarsDialog 的构造函数移动到让我们 LoadData 的方法。在 ShowDialog 之前调用 LoadData。您可以删除行 lvCars.ItemsSource = (from c in ctx.Cars select c).ToList<Car>(); 并仅保留第二个查询。

CarsDialog 后面的代码:

public CarsDialog()
{
    InitializeComponent();
}

public void LoadData()
{
    ctx = new CarDbContext();
    int idNum = Convert.ToInt32(lbId.Content);
    lvCars.ItemsSource = (from c in ctx.Cars where c.Owner.Id == idNum select c).ToList<Car>();
}

MainWindow 后面的代码:

private void btManage_Click(object sender, RoutedEventArgs e)
{
    var selected = lvOwnerCar.SelectedItem as Owner;
    int selectedId = selected.Id;
    string selectedName = selected.Name;
    CarsDialog dlg = new CarsDialog();
    dlg.lbName.Content = selectedName;
    dlg.lbId.Content = selectedId;
    dlg.Owner = this;
    dlg.LoadData();
    dlg.ShowDialog();
}

最大的错误是违反 OOP 原则。 window 的所有 UI 元素都是其实现的私有细节,必须封装。 对于 public 通信,您必须声明 public 方法和属性。

还有一个更小的细节。 为什么要使用 Label 而不是 TextBlock 来显示纯文本?

一个比较正确的实现示例(与问题相关的部分代码):

public class CarsDialog
{
    public CarsDialog()
    {
        InitializeComponent();
        ctx = new CarDbContext();
        lvCars.ItemsSource = (from c in ctx.Cars select c).ToList<Car>();
    }

    private int _idNum;
    public int IdNum
    {
        get => _idNum;
        set
        {
            lbId.Content = _idNum = value;
            ctx = new CarDbContext();
            lvCars.ItemsSource = (from c in ctx.Cars where c.Owner.Id == idNum select c).ToList<Car>();
        }
    }

    private string _nameCar;
    public string NameCar
    {
        get => _nameCar;
        set
        {
            lbName.Content = _nameCar = value;
        }
    }
}

使用:

    private void btManage_Click(object sender, RoutedEventArgs e)
    {
        var selected = lvOwnerCar.SelectedItem as Owner;
        int selectedId = selected.Id;
        string selectedName = selected.Name;
        CarsDialog dlg = new CarsDialog();
        dlg.NameCar = selectedName;
        dlg.IdNum = selectedId;
        dlg.Owner = this;
        dlg.ShowDialog();
    }