C# 未绘制数据模板,wpf

C# 未绘制数据模板,wpf,c#,wpf,C#,Wpf,我有以下代码试图显示我的数据上下文的属性。 但是当我运行应用程序时,我什么也看不到。 我是WPF的初学者。 我可能错过了一些小背景 如果我删除DataContext=“{Binding RelativeSource={RelativeSource Self}}”并将代码隐藏中的DataContext设置为MyPerson属性并设置 那么一切都很好 <Window x:Class="WpfTestApp.MainWindow" xmlns="http://schemas.mi

我有以下代码试图显示我的数据上下文的属性。 但是当我运行应用程序时,我什么也看不到。 我是WPF的初学者。 我可能错过了一些小背景

如果我删除
DataContext=“{Binding RelativeSource={RelativeSource Self}}”
并将代码隐藏中的DataContext设置为MyPerson属性并设置

那么一切都很好

<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));
                }
            }
        }

公共部分类主窗口:窗口
{
公共人物MyPerson{get;set;}
公共主窗口()
{
初始化组件();
MyPerson=新人{Age=30,Name=“David”};
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
显示(“按钮被点击!!!”;
}       
}
公共类人员:INotifyPropertyChanged
{
私人互联网;
私有字符串\u名称;
公共信息
{
得到
{
回归年龄;
}
设置
{
如果(值!=\u年龄)
{
_年龄=价值;
不动产变更(“年龄”);
}
}
}
公共字符串名
{
得到
{
返回_name;
}
设置
{
如果(值!=\u名称)
{
_名称=值;
不动产变更(“名称”);
}
}
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}

您在
初始化组件
之后创建
MyPerson
,它还将
数据上下文
设置为
主窗口
,如XAML中所指定,并且由于您的属性未引发
INotifyPropertyChanged。PropertyChanged
事件UI从未被通知其已更改。您可以在
main窗口上实现
INotifyPropertyChanged
,并为
MyPerson
属性引发
PropertyChanged
事件,或者如果该属性没有更改,只有其属性更改,则颠倒顺序

public MainWindow()
{
    MyPerson = new Person { Age = 30, Name = "David" };
    InitializeComponent();        
}

主题外但这是一个使用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));
            }
        }
    }
视图和视图模型绑定

<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>


希望这有帮助

尝试在InitializeComponent()上设置MyPerson属性;因为你是wpf的初学者。请看一看