【问题标题】:How to get WPF DataBinding-to-an-object to work如何让 WPF DataBinding-to-an-object 工作
【发布时间】:2009-05-11 15:32:40
【问题描述】:

在以下示例中,我通过 ObjectDataProvider 将 XAML 绑定到静态对象。 当用户更改信息时,我希望它自动反映在 XAML 中。

我不明白的是:

  • 如何使对象永久化?我必须创建一个单身人士吗?在点击事件中,如何访问“正在编辑的对象”
  • 当然我最终当然希望从读取 XML 文件或 Web 服务的模型中检索数据,并且 我当然希望我的 ViewModel 每隔一秒左右检查我的模型以查看数据是否已更改并将其反映在 XAML 中

我如何从这里到达那里:

XAML:

<Window x:Class="TestBinding99382.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestBinding99382"
    Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <ObjectDataProvider 
             x:Key="DataSourceCustomer" 
             ObjectType="{x:Type local:Customer}" MethodName="GetCustomer"/>

        <Style x:Key="DataRowStyle" TargetType="StackPanel">
            <Setter Property="Orientation" Value="Horizontal"/>
            <Setter Property="VerticalAlignment" Value="Top"/>
            <Setter Property="Margin" Value="0 10 0 0"/>
            <Setter Property="DataContext" 
                    Value="{StaticResource DataSourceCustomer}"/>
            <Setter Property="DockPanel.Dock" Value="Top"/>
        </Style>
    </Window.Resources>

    <DockPanel>
        <StackPanel DockPanel.Dock="Top" 
                    DataContext="{StaticResource DataSourceCustomer}" 
                    Orientation="Horizontal">
            <TextBlock Text="{Binding Path=FirstName}"/>
            <TextBlock Text=" "/>
            <TextBlock Text="{Binding Path=LastName}"/>
            <TextBlock Text=" ("/>
            <TextBlock Text="{Binding Path=FullName}" FontWeight="Bold"/>
            <TextBlock Text=")"/>
        </StackPanel>

        <StackPanel Style="{StaticResource DataRowStyle}">
            <TextBlock Text="First Name:"/>
            <TextBox Text="{Binding Path=FirstName}" 
                      Width="200" Margin="3 0 0 0"/>
        </StackPanel>

        <StackPanel Style="{StaticResource DataRowStyle}">
            <TextBlock Text="Last Name:"/>
            <TextBox Text="{Binding Path=LastName}" 
                     Width="200" Margin="3 0 0 0"/>
        </StackPanel>

        <StackPanel Style="{StaticResource DataRowStyle}">
            <Button Content="Save Changes" Click="Button_Click"/>
        </StackPanel>

    </DockPanel>
</Window>

代码隐藏:

using System.Windows;
using System.ComponentModel;
using System;

namespace TestBinding99382
{
    public partial class Window1 : Window
    {
        private Customer _customer;

        public Window1()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //I want to edit the _customer object here
            //and have it my changes automatically reflect in my XAML
            //via the INotifyPropertyChanged inheritance.
        }
    }

    public class Customer : INotifyPropertyChanged
    {
        private string _firstName;
        private string _lastName;

        public string FirstName
        {
            get
            {
                return _firstName;
            }

            set
            {
                _firstName = value;
                this.RaisePropertyChanged("FirstName");
                this.RaisePropertyChanged("FullName");
            }
        }

        public string LastName
        {
            get
            {
                return _lastName;
            }

            set
            {
                _lastName = value;
                this.RaisePropertyChanged("LastName");
                this.RaisePropertyChanged("FullName");
            }

        }

        public string FullName
        {
            get
            {
                return String.Format("{0} {1}", _firstName, _lastName);
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }


        public static Customer GetCustomer()
        {
            return new Customer { FirstName = "Jim", LastName = "Smith" };
        }

    }
}

【问题讨论】:

    标签: wpf xaml data-binding


    【解决方案1】:

    在点击事件中,如何访问 “正在编辑的对象”

    您可以使用 FindResource 方法访问后台代码中的资源,见下文。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        ObjectDataProvider objectDataProvider 
            = FindResource("DataSourceCustomer") as ObjectDataProvider;
        _customer = objectDataProvider.Data as Customer;
    }
    

    关于您的其他问题:

    什么是永续?如果这是您的问题,您不必在 WPF 中创建单例数据绑定。

    当然,我最终当然希望从读取 XML 文件或 Web 服务的模型中检索数据,当然我希望我的 ViewModel 每隔一秒左右检查一次我的模型,以查看数据是否已更改并反映这一点在 XAML 上。

    WPF 数据绑定将自动更新您使用 INotifyPropertyChanged 对象的视图。除非出于性能原因,您只想每秒更新一次视图,否则只需坚持正常的数据绑定即可。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-27
    • 2014-02-04
    • 2012-01-02
    • 2017-01-08
    • 1970-01-01
    • 2013-01-09
    相关资源
    最近更新 更多