首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在c#/WPF (DataContext = obj)中绑定子类(绑定subclass.var}

在c#/WPF (DataContext = obj)中绑定子类(绑定subclass.var}
EN

Stack Overflow用户
提问于 2018-07-21 21:10:19
回答 1查看 267关注 0票数 0

我想在c#/WPF中使用数据绑定。创建一个对象并将其用作datacontext已经可以处理它的变量。

但是我想在datacontext对象中创建类并使用这些变量。

正确的绑定应该与....{Binding Path=Eyeobj.Farbe}....

问题出在DataContenxt、类还是WPFs这一方?

MainWindow.xaml.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DataContextDemo
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Person obj = new Person()
        {
            FirstName = "John",
            LastName = "Smith",
            Age = 30
        };



    public MainWindow()
    {

        InitializeComponent();



        this.DataContext = obj;
    }


}
}

MainWindow.xaml

代码语言:javascript
复制
<Window x:Class="DataContextDemo.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:DataContextDemo"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="80"/>
        <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>

    <TextBlock Margin="4" Text="First Name" VerticalAlignment="Center"/>
    <TextBox Margin="4" Text="{Binding Path= FirstName}" Grid.Column="1"/>

    <TextBlock Margin="4" Text="Last Name" Grid.Row="1" VerticalAlignment="Center"/>
    <TextBox Margin="4" Text="{Binding Path= LastName}" Grid.Column="2" Grid.Row="1"/>

Person.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace DataContextDemo
{
public class Person : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public Augen Eyeobj = new Augen("Red");




    //public PropertyChangedEventHandler handler = PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }



    public string FirstName
    {
        get
        {
            return this.firstName;
        }
        set
        {
            this.firstName = value;
            NotifyPropertyChanged();
        }
    }

    public string LastName
    {
        get
        {
            return this.lastName;
        }
        set
        {
            this.lastName = value;
            NotifyPropertyChanged();
        }
    }

    public int Age
    {
        get
        {
            return this.age;
        }
        set
        {
            this.age = value;
            NotifyPropertyChanged();
        }
    }

    private string firstName { get; set; }
    public string lastName { get; set; }
    public int age { get; set; }
}
}



    <TextBlock Margin="4" Text="Age" Grid.Row="2" VerticalAlignment="Center"/>
    <TextBox Margin="4" Text="{Binding Path = Eyeobj.Farbe}" Grid.Column="3" Grid.Row="2"/>

    <TextBlock Margin="3" Text="{Binding Path=Eyeobj.Farbe}" Grid.Row="2" Grid.Column="3" x:Name="testbox"></TextBlock>

    <Button Margin="4" Grid.Row="3" Click="Button_Click"></Button>





</Grid>
</Window>

Augen.cs

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace DataContextDemo
{
public class Augen : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string farbe = "Blau";

    public Augen(string farbe)
    {
        Farbe = farbe;
    }


    //public PropertyChangedEventHandler handler = PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public string Farbe
    {
        get
        {
            return this.farbe;
        }
        set
        {
            this.farbe = value;
            NotifyPropertyChanged();
        }
    }


}
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-21 21:32:29

您将Eyeobj用作字段-它在视图中是不可见的。使该属性实现INotifyPropertyChanged,就像您对FirstName所做的那样。

代码语言:javascript
复制
private Augen _eyeobj;
public Augen Eyeobj 
      {
        get
        {
            return _eyeobj;
        }
        set
        {
         if(_eyeobj != value)
         {
            _eyeobj  = value;
            NotifyPropertyChanged("Eyeobj");
         }
        }
       }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51456410

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档