我想在c#/ WPF中使用数据绑定。创建对象并将其用作数据上下文已经可以使用其变量。
但是我想在datacontext对象中创建类并使用这些变量。
正确的绑定应该与.... {Binding Path = Eyeobj.Farbe} ....
是DataContenxt,类还是WPF方面的问题?
MainWindow.xaml.cs
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
<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
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
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();
}
}
}
}
答案 0 :(得分:0)
您将Eyeobj
用作字段-从视图中看不到它。像创建FirstName
一样,制作属性,实现INotifyPropertyChanged
。
private Augen _eyeobj;
public Augen Eyeobj
{
get
{
return _eyeobj;
}
set
{
if(_eyeobj != value)
{
_eyeobj = value;
NotifyPropertyChanged("Eyeobj");
}
}
}