如何使WPF DataBinding-to-an-object工作

时间:2009-05-11 15:32:40

标签: wpf xaml data-binding

在下面的示例中,我通过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" };
        }

    }
}

1 个答案:

答案 0 :(得分: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对象的视图。除非出于性能原因,否则您只需要每隔一秒左右更新一次视图,只需坚持正常的数据绑定即可。