[英]C# WPF binding doesn't take data from Property
In my XAML I am doing the following在我的 XAML 中,我正在执行以下操作
<Label Content="{Binding ElementName=Root, Path=UserData.Email, Mode=OneWay}" />
the Root
element is my Window
itself and the UserData
Is a get; private set;
Root
元素是我的Window
本身,而UserData
是一个get; private set;
get; private set;
auto property in my codebehind file, the Email
property is get-only and is of type string
. auto 属性在我的代码隐藏文件中, Email
属性是 get-only 并且是string
类型。
the UserData
object gets set after the user has logged in. But the binding is not taking the value from the object. UserData
对象在用户登录后设置。但绑定没有从对象中获取值。 I have verified that the object does indeed contain the correct data and isn't null
.我已经验证该对象确实包含正确的数据并且不是null
。 What am I missing here?我在这里缺少什么?
I went ahead and created a hello world version for this.我继续为此创建了一个 hello world 版本。 Here is the xml.这是xml。 This should simply change the banner when the button is clicked to the text in the text box.当单击按钮到文本框中的文本时,这应该简单地更改横幅。 I couldn't find a super simple example so I just made one.我找不到一个超级简单的例子,所以我只做了一个。 Obviously there are way more advanced ways to do this but it should make for a simple version to build from.显然有更高级的方法可以做到这一点,但它应该是一个简单的版本来构建。
<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>
Next is the ModelView that implements the INotifyPropertyChanged
interface.接下来是实现INotifyPropertyChanged
接口的INotifyPropertyChanged
。 Note that your properties must be public properties with a getter, setter and backing field.请注意,您的属性必须是具有 getter、setter 和支持字段的公共属性。 This allows you to call the OnPropetyChanged()
method whenever the property is set.这允许您在设置属性时调用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));
}
}
}
Lastly the MainWindow.最后是主窗口。 Set the DataContext
in the main constructor.在主构造函数中设置DataContext
。 Note I could have set the DataContext
of the main grid and all of its children would inherit the same DataContext
.注意我可以设置主网格的DataContext
并且它的所有子DataContext
都将继承相同的DataContext
。 This would keep you from having to set all of the components' individually.这将使您不必单独设置所有组件。
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.