【问题标题】:Unable to set DataContext through XAML, but the same can be set through Code无法通过 XAML 设置 DataContext,但同样可以通过 Code 设置
【发布时间】:2012-08-08 10:01:02
【问题描述】:

当我在 XAML 中声明它时,Datacontext 不起作用。但如果在 Code 中设置,则同样有效。

详细分析。

我的 XAML

    <Window x:Class="SimpleDatabindingwithclass.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" Name="windo">
    <Grid DataContext="{Binding ElementName=windo,Path=objectOfStudent}">
    <Grid.RowDefinitions>
        <RowDefinition Height="*"></RowDefinition>
    </Grid.RowDefinitions>
    <TextBox Grid.Row="0" Margin="25" Height="25" Width="100" HorizontalAlignment="Left" Name="TextBox1" Text="{Binding Path=StudentName}"></TextBox>

</Grid>
</Window>

对应的代码。

    namespace SimpleDatabindingwithclass
    {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
         public MainWindow()
         {
              InitializeComponent();
              Student objectOfStudent = new Student();
              objectOfStudent.StudentName = "John diley";
              objectOfStudent.Address = "20, North Travilia, Washington DC.";
              //not setting datacontext here since i set that in xaml
         }
         public class Student
         {
             private string studentname;
             private string address;

             public string Address
             {
                 get { return address; }
                 set { address = value; }
        }
        public string StudentName
        {
            get{return studentname;}
            set{studentname = value;}
        }
    }
}

}

但是,当我使用这个 XAML 并通过代码设置 datacontext 时,它也是一样的!

ie,当我放类似

的东西时
     this.DataContext = objectOfStudent; 

在 MainWindow() 中,应用程序工作正常! 你觉得问题出在哪里?

【问题讨论】:

    标签: c# wpf xaml datacontext


    【解决方案1】:

    绑定仅适用于公共属性,您不能绑定到某些局部变量。将objectOfStudent 设为MainWindow 的公共财产。

    编辑:

    public Student objectOfStudent { get; set; }
    
    public MainWindow()
    {
        InitializeComponent();
        objectOfStudent = new Student();
        objectOfStudent.StudentName = "John diley";
        objectOfStudent.Address = "20, North Travilia, Washington DC.";
        //not setting datacontext here since i set that in xaml
     }
    

    编辑:

    您还需要在MainWindowStudent 类中实现INotifyPropertyChanged 接口,并在设置属性时引发PropertyChanged。这是正确的方法,每次更改属性时绑定都会更新。或者一个简单的方法:在调用InitializeComponent之前创建objectOfStudent

    public MainWindow()
    {
        objectOfStudent = new Student();
        objectOfStudent.StudentName = "John diley";
        objectOfStudent.Address = "20, North Travilia, Washington DC.";
        InitializeComponent();
        //not setting datacontext here since i set that in xaml
     }
    

    【讨论】:

    • 谢谢扎巴斯基。我知道 objectofStudent 与 Student 类有关,是一个局部变量。现在,我如何使它成为 MainWindow 的属性?简单地说,如何添加一个属性并将其引用到 Student 实例?
    • 那仍然没有工作 Zabavsky!仍然两个文本字段都是空的!添加 public Student objectOfStudent { get;放;我在上面的 XAML 代码中使用了
    • Zabavsky @感谢您的输入。仍然没有做对。无论如何,问题定义很明确。
    • @Madhuresh 了解更多关于INotifyPropertyChanged 接口及其用途的信息。
    【解决方案2】:

    这是因为您试图通过 XAML 使用名为 objectOfStudent局部变量 - 这在 XAML 上下文中没有任何意义。 XAML 只接受字段和属性,不接受局部变量。

    【讨论】:

    • 这是否意味着我不能在 XAML 中使用类的对象?如何将其移植到 XAML 而不是将其放入代码?对不起,如果这听起来很傻,但这有点吓到我了!
    • 您可以将一个名为“Student”的属性添加到您的 Window 类中,该属性返回一个 Student insatnce,然后使用它来代替 'objectOfStudent'
    猜你喜欢
    • 2020-12-10
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2011-12-07
    • 2010-12-02
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多