C# WPF 绑定(bind)不从 Property 获取数据

标签 c# wpf xaml binding

在我的 XAML 中,我正在执行以下操作

<Label Content="{Binding ElementName=Root, Path=UserData.Email, Mode=OneWay}" />

Root 元素是我的 Window 本身,UserData 是一个 get; private set; auto 属性在我的代码隐藏文件中,Email 属性是仅获取的且类型为 string

UserData 对象在用户登录后设置。但绑定(bind)并未从该对象获取值。我已验证该对象确实包含正确的数据并且不是 null。我在这里缺少什么?

最佳答案

我继续为此创建了一个 hello world 版本。这是 xml。当单击按钮时,这应该只是将横幅更改为文本框中的文本。我找不到一个 super 简单的例子,所以我就做了一个。显然,有更高级的方法可以做到这一点,但它应该可以构建一个简单的版本。

<Window x:Class="Hello_World.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Name="MyLabel" Content="{Binding MyLabel}" HorizontalAlignment="Left" Margin="58,37,0,0" VerticalAlignment="Top" Height="65" Width="423" FontSize="44"/>
        <TextBox Name="MyTextBox" HorizontalAlignment="Left" Height="28" Margin="163,162,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="163"/>
        <Button Content="Change Banner" HorizontalAlignment="Left" Margin="251,209,0,0" VerticalAlignment="Top" Width="109" Click="Button_Click"/>

    </Grid>
</Window>

接下来是实现 INotifyPropertyChanged 接口(interface)的 ModelView。请注意,您的属性必须是具有 getter、setter 和支持字段的公共(public)属性。这允许您在设置属性时调用 OnPropetyChanged() 方法。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Hello_World
{
    public class MainViewModel: INotifyPropertyChanged
    {


        private string _myLabel;

        public string MyLabel
        {
            get { return _myLabel; }
            set
            {
                _myLabel = value;
                OnPropertyChanged(nameof(MyLabel));
            }
        }    

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propetyName)
        {
            if(PropertyChanged != null)
            PropertyChanged(this,new PropertyChangedEventArgs(propetyName));

        }

    }
}

最后是主窗口。在主构造函数中设置DataContext。请注意,我可以设置主网格的 DataContext,并且其所有子网格都将继承相同的 DataContext。这将使您不必单独设置所有组件。

namespace Hello_World
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MainViewModel MyViewModel;


        public MainWindow()
        {
            InitializeComponent();
            MyViewModel = new MainViewModel();

            // Here's where I'm setting the object to look at.
            DataContext = MyViewModel;

            // Now I don't need to access the textbox directly.
            MyViewModel.MyLabel = "Hello World";    
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Note: ICommand is a more advanced topic.
            MyViewModel.MyLabel = MyTextBox.Text;
        }
    }
}

关于C# WPF 绑定(bind)不从 Property 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37362216/

相关文章:

c# - 无法通过 IP 地址连接到 SQL Server Express - c#

c# - 如何从已转换为对象的匿名类型中获取数据?

c# - 带斜杠的正则表达式模式

c# - 哪个监视器处于事件状态?

c# - 验证失败时禁用保存按钮

c# - Xamarin Android TextColor 属性在不同设备上表现异常

java - 使用Java,如何获取TextBlock文本的 'ActualWidth'并使用它?

c# - 从提升的子进程中获取错误和标准输出

wpf - 将 WPF DataGridComboBoxColumn 与 MVVM 结合使用 - 绑定(bind)到 ViewModel 中的属性

c# - 生成除黑色之外的随机颜色