不绘制数据模板,wpf

本文关键字:wpf 绘制 数据 | 更新日期: 2023-09-27 18:31:47

我有以下代码,我正在尝试显示数据上下文的属性。但是当我运行应用程序时,我什么也没看到。我是 WPF 的初学者。我可能在某处错过了一些小设置。

如果我删除DataContext="{Binding RelativeSource={RelativeSource Self}}"并将代码中的数据上下文隐藏设置为 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));
                }
            }
        }

不绘制数据模板,wpf

InitializeComponent 之后创建MyPerson,这也会将DataContext设置为 XAML 中指定的MainWindow,并且由于你的属性不会引发INotifyPropertyChanged.PropertyChanged因此永远不会通知事件 UI 它已更改。您可以在MainWindow上实现INotifyPropertyChanged并为MyPerson属性引发PropertyChanged事件,或者如果该属性未更改,则仅其属性更改,则颠倒顺序

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

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

和视图模型绑定

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

希望这有帮助!!!