簡體   English   中英

C# WPF 綁定不從屬性中獲取數據

[英]C# WPF binding doesn't take data from Property

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

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

Root元素是我的Window本身,而UserData是一個get; private set; get; private set; auto 屬性在我的代碼隱藏文件中, Email屬性是 get-only 並且是string類型。

UserData對象在用戶登錄后設置。但綁定沒有從對象中獲取值。 我已經驗證該對象確實包含正確的數據並且不是null 我在這里缺少什么?

我繼續為此創建了一個 hello world 版本。 這是xml。 當單擊按鈕到文本框中的文本時,這應該簡單地更改橫幅。 我找不到一個超級簡單的例子,所以我只做了一個。 顯然有更高級的方法可以做到這一點,但它應該是一個簡單的版本來構建。

<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接口的INotifyPropertyChanged 請注意,您的屬性必須是具有 getter、setter 和支持字段的公共屬性。 這允許您在設置屬性時調用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都將繼承相同的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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM