【问题标题】:WPF binding label content with class parameter带有类参数的 WPF 绑定标签内容
【发布时间】:2018-05-20 21:02:48
【问题描述】:

我已经阅读了网上的大量教程、MSDN 上的文档和此处的答案,但我仍然不了解 WPF 中的绑定。我有自己的这样的 C# 类

namespace Organizer

{

public class UIDate
{
    private int year;
    private int month;
    private String dateStr;

    public UIDate()
    {
        DateTime actualDateTime = DateTime.Now;
        year = actualDateTime.Year;
        month = actualDateTime.Month;
        dateStr = Convert.ToString(Year);
    }

    public int Year
    {
        get { return year; }
        set { year = value; }
    }

    public int Month
    {
        get { return month; }
        set { month = value; }
    }

    public String DateStr
    {
        get { return dateStr; }
        set { dateStr = value; }
    }
}

}

我想在这里将属性 DateStr 与标签内容绑定

<Window x:Class="Organizer.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" WindowState="Normal">
<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <Button MinWidth="25" MaxHeight="25" Margin="8"/>
        <Button MinWidth="15" MaxHeight="15" Margin="5"/>
        <Label Name="UIDateYM" Content="{Binding Path=UIDate.DateStr, UpdateSourceTrigger=PropertyChanged}" Margin="0, 8"/>

我相信 XAML 的其余部分无关紧要。

还有 xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Organizer;

namespace Organizer
{
    /// <summary>
    /// Logika interakcji dla klasy MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {            
            InitializeComponent();
            UIDate uiDate = new UIDate();
            this.DataContext = uiDate;
        }
    }
}

当应用程序运行时,标签中没有 txt。内容为空。 我检查了我的 uiDate 对象,它可以工作。如我所愿,属性及其字段具有正确的值。如何绑定它们?我错过了什么?

谢谢。

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:
    1. 你必须设置一个数据上下文 (ok)= -> (你已经做到了,如果你使用 MVVM 有更优雅的方法,但没关系)
      1. 您已在 xaml 中设置了绑定,但您正在绑定到类名 UIDate.DateStr

    如果您将数据上下文设置为元素。所有子节点都将具有相同的数据上下文 -> 只要您没有更改它。 (您将其设置为 window / this,因此作为子标签的标签也具有相同的数据上下文)

    请记住,您的 DataContext 已经设置好,您可以直接访问其中的属性。

    改变:

    {Binding Path=UIDate.DateStr, UpdateSourceTrigger=PropertyChanged}
    

    到:

    {Binding Path=DateStr}
    

    这里的 updatesourcetrigger 不是必需的。贝克。触发器在这里通知有界对象发生了变化。 (仅在这个方向)。

    在您的情况下,标签不是输入控件,只需要显示值。意味着控件不需要通知绑定的对象发生了变化。

    下一点: 如果您不打算更改对象中的值。您可以保持代码原样。 贝克。如果您设置了一个对象(在您的情况下您设置了数据上下文),那么所有引用该对象的绑定都会被更新。这样就设置了初始值。

    但是如果你在对象已经绑定的时候改变了一个值,你需要通知Control。你应该寻找接口 INotifyPropertyChanged ...

    欢迎希望它现在清楚

    【讨论】:

    • 谢谢!现在我了解绑定是如何工作的。确定 UpdateSourceTrigger 会这样做,但它的工作方向相反。我想我必须熟悉 MVVM 模式。
    • 不客气。经过多年使用 wpf 并在大型业务应用程序中使用它的经验,我建议使用 MVVM 模式,它需要更多的初步工作,但在 90% 的情况下绝对值得。
    猜你喜欢
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    相关资源
    最近更新 更多