我有以下代码,我试图显示我的数据上下文的属性。 但是当我运行应用程序时,我什么也看不见。 我是WPF的初学者。 我可能在某个地方错过了一些小设置。
如果我删除DataContext="{Binding RelativeSource={RelativeSource Self}}"
并将代码中的DataContext设置为MyPerson属性并设置
<ContentControl x:Name="myCC" Content="{Binding }"></ContentControl>
然后一切正常。
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTestApp"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="_Name:" Grid.Row="0" Grid.Column="0"></Label>
<TextBox Text="{Binding Path=Name}" Grid.Column="1" Grid.Row="0"></TextBox>
<Label Content="_Age:" Grid.Row="1" Grid.Column="0"></Label>
<TextBox Text="{Binding Path=Age}" Grid.Column="1" Grid.Row="1"></TextBox>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid >
<ContentControl x:Name="myCC" Content="{Binding Path=MyPerson}"></ContentControl>
</Grid>
</Window>
public partial class MainWindow : Window
{
public Person MyPerson { get; set; }
public MainWindow()
{
InitializeComponent();
MyPerson = new Person { Age = 30, Name = "David" };
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Button is clicked!!!");
}
}
public class Person : INotifyPropertyChanged
{
private int _age;
private string _name;
public int Age
{
get
{
return _age;
}
set
{
if (value != _age)
{
_age = value;
OnPropertyChanged("Age");
}
}
}
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
答案 0 :(得分:1)
您在MyPerson
之后创建InitializeComponent
,它也会按照XAML中的指定将DataContext
设置为MainWindow
,并且因为您的属性未引发INotifyPropertyChanged.PropertyChanged
事件UI永远不会被告知它已经改变。您可以在INotifyPropertyChanged
上实施MainWindow
并为PropertyChanged
属性提升MyPerson
事件,或者如果该属性未发生更改,则只更改其属性,撤消订单
public MainWindow()
{
MyPerson = new Person { Age = 30, Name = "David" };
InitializeComponent();
}
答案 1 :(得分:0)
OFF主题,但使用 MVVM模式
的简单示例您的模型类
public class Person : INotifyPropertyChanged
{
private int _age;
private string _name;
public int Age
{
get
{
return _age;
}
set
{
if (value != _age)
{
_age = value;
OnPropertyChanged("Age");
}
}
}
public string Name
{
get
{
return _name;
}
set
{
if (value != _name)
{
_name = value;
OnPropertyChanged("Name");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ViewModel is a Wrapper around the Model
public class PersonViewModel : INotifyPropertyChanged
{
private Person myPerson;
public Person MyPerson
{
get
{
if(myPerson == null)
myPerson=new Person { Age = 30, Name = "David" };
}
set
{
myPerson=value;
OnPropertyChanged("MyPerson");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
查看和ViewModel绑定
<Window x:Class="WpfTestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfTestApp"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:PersonViewModel />
</Window.DataContext>
<Window.Resources>
<DataTemplate DataType="{x:Type local:Person}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Content="_Name:" Grid.Row="0" Grid.Column="0"></Label>
<TextBox Text="{Binding Path=Name}" Grid.Column="1" Grid.Row="0"></TextBox>
<Label Content="_Age:" Grid.Row="1" Grid.Column="0"></Label>
<TextBox Text="{Binding Path=Age}" Grid.Column="1" Grid.Row="1"></TextBox>
</Grid>
</DataTemplate>
</Window.Resources>
<Grid >
<ContentControl x:Name="myCC" Content="{Binding Path=MyPerson}"></ContentControl>
</Grid>
希望这有助于!!!!