如何在代码中访问XAML绑定类属性? (C#,WPF表格)

时间:2017-06-30 11:14:02

标签: c# wpf xaml data-binding

在下面的示例中,我使用XAML将DataContext设置为类名,而不是对象名(据我所知)。绑定有效 - txtEcho文本框显示当txtName文本框失去焦点时输入的内容。我希望能够在代码隐藏中访问此类的属性,因此我需要一个对象引用。我可以创建一个Person对象并将DataContext设置为该对象,但我正在咨询的书中的模式不会这样做 - 使用类名并且在这些教科书的示例中没有显式创建对象。

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication5"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:Person x:Key="personData" />
    </Window.Resources>
    <Grid>
        <Grid.DataContext>
            <Binding Source="{StaticResource personData}" />
        </Grid.DataContext>
        <TextBlock HorizontalAlignment="Left" Margin="27,41,0,0" TextWrapping="Wrap" Text="Name" VerticalAlignment="Top" Height="23"/>
        <TextBox x:Name="txtName" Text="{Binding Name}" HorizontalAlignment="Left" Height="23" Margin="78,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <TextBox x:Name="txtEcho" Text="{Binding Name}" HorizontalAlignment="Left" Height="23" Margin="262,42,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>

    </Grid>
</Window>

代码是

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //txtFreedom.Text = <what exactly?>.Name; // no object reference!
        }
    }
    class Person : INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
                OnPropertyChanged("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您可以像这样访问您在XAML中创建的<type>实例:

Person

答案 1 :(得分:0)

我找到了一种方法,mm8的帮助。代码现在是

namespace WpfApplication5
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public static Person oPerson;

        public MainWindow()
        {
            InitializeComponent();
            oPerson = this.Resources["personData"] as Person;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            txtFreedom.Text = oPerson.Name;
        }
    }

    public class Person : INotifyPropertyChanged
    {
        string name;
        public string Name
        {
            get { return this.name; }
            set
            {
                this.name = value;
                OnPropertyChanged("Name");
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

不同之处在于,声明一个公共静态变量来保存Person对象。

public static Person oPerson;

在MainWindow()方法中,将其设置为幕后的Person实例。

 oPerson = this.Resources["personData"] as Person;

我必须公开这个类来停止编译器可访问性异议。

public class Person : INotifyPropertyChanged

现在我可以引用实例的属性。

    private void Button_Click(object sender, RoutedEventArgs e)
    {
         txtFreedom.Text = oPerson.Name;
    }