简体   繁体   English

如何将代码隐藏中的自定义对象绑定到XAML中的网格?

[英]How can I bind a custom object in code-behind to a Grid in XAML?

In the following example, the Message property binds correctly but the FirstName, LastName and Age properties of the Customer object are blank. 在下面的示例中,Message属性正确绑定,但是Customer对象的FirstName,LastName和Age属性为空。 Why is that? 这是为什么?

XAML: XAML:

<Window x:Class="TestBinding9922.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel>

        <TextBlock Text="{Binding Message}"/>

        <Grid DataContext="{Binding Customer}" >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <Label Grid.Column="0" Grid.Row="0" Content="First Name:" />
            <Label Grid.Column="0" Grid.Row="1" Content="Last Name:" />
            <Label Grid.Column="0" Grid.Row="2" Content="Age:" />

            <TextBox Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" />
            <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" />
            <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Age}" />

        </Grid>

    </StackPanel>
</Window>

Code-Behind: 代码隐藏:

using System.Windows;
using System.ComponentModel;

namespace TestBinding9922
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public string Message { get; set; }
        public Customer Customer { get; set; }

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Message = "this works";

            Customer customer = new Customer() { FirstName = "Jim", LastName = "Smith", Age = 45 };
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
}

Addendum: 附录:

Even when I use INotifyPropertyChanged, the textboxes are still blank: 即使使用INotifyPropertyChanged,文本框仍为空白:

using System.Windows;
using System.ComponentModel;

namespace TestBinding9922
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion

        #region ViewModelProperty: Customer
        private Customer _customer;
        public Customer Customer
        {
            get
            {
                return _customer;
            }

            set
            {
                _customer = value;
                OnPropertyChanged("Customer");
            }
        }
        #endregion

        public Window1()
        {
            InitializeComponent();
            DataContext = this;

            Message = "this works";

            Customer customer = new Customer() { FirstName = "Jim", LastName = "Smith", Age = 45 };
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }

}

Your Customer class needs to implement INotifyPropertyChanged and subsequently the properties that are being changed need to notify when they get changed. 您的Customer类需要实现INotifyPropertyChanged,随后要更改的属性需要在更改时通知。

You can find an example of this here , among other places. 在其他位置,您可以在这里找到一个示例。

Also, you're not instantiating your Customer property, you're creating a local variable; 另外,您没有实例化Customer属性,而是在创建局部变量。 it should be: 它应该是:

Customer = new Customer() 
    { FirstName = "Jim", LastName = "Smith", Age = 45 };

看起来您是将新的Customer实例分配给本地变量“ customer”,而不是Window的属性。

You could remove the setting of the DataContext for the grid in the XAML, and then bind the textboxes to Customer.FirstName, etc. 您可以在XAML中删除网格的DataContext设置,然后将文本框绑定到Customer.FirstName等。

Or, you could remove the setting of the DataContext for the grid in the XAML, as before, but give the grid a name, and then set its DataContext in the code-behind, like you did for the window. 或者,您可以像以前一样在XAML中删除网格的DataContext设置,但是给网格命名,然后像在窗口中一样在后面的代码中设置其DataContext。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM