【问题标题】:Data Binding INotifyPropertyChanged Not Working as Expected数据绑定 INotifyPropertyChanged 未按预期工作
【发布时间】:2019-04-23 00:03:36
【问题描述】:

我正在学习数据绑定,尤其是DataGrid。在我的代码中,我有一个DataGrid 和一个Label,它显示了DataGrid 的第一个单元格值。 XAML 的输出类似于。考虑下图,第一个单元格值旁边的标签内容是: Monkey,我想我是从 DataGrid 第一个单元格中得到的。现在我想要更新 The First Cell Value is: 的左侧,当我更改 DataGrid 第一个单元格中的值时。但我无法实现它。

下面是我的代码和 XAML 文件

代码

namespace DataGridExampleSelfTry
{
    public class MainWindowVM:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
        private string _FirstCell;

        public string FirstCell
        {
            get{ return _FirstCell; }

            set
            {
                _FirstCell = value;
                PropertyChanged(this,new PropertyChangedEventArgs(nameof(FirstCell)));
            }

        }
        public string SecondCell { get; set; }

        private ObservableCollection<animies> _animelistforbinding;
        public ObservableCollection<animies> animelistforbinding
        { get
            {
                return _animelistforbinding;
            }
          set
            {
                _animelistforbinding = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(animelistforbinding)));
            }
        }
        ObservableCollection<animies> addinganime = new ObservableCollection<animies>();

        public MainWindowVM()
        {
            addinganime.Add(new animies("Monkey", "D Luffy"));

            animelistforbinding = addinganime;

            FirstCell = animelistforbinding[0].FirstName;
            SecondCell = animelistforbinding[0].LastName;   
        }     
    }

    public class animies:INotifyPropertyChanged
    {
        private string _FirstName;

        public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };

        public string FirstName
        {
            get { return _FirstName; }
            set
            {
                _FirstName = value;
                PropertyChanged(this, new PropertyChangedEventArgs(nameof(FirstName)));
            }
        }
        public string LastName { get; set; }
        public animies(string dFirstName, string dLastName)
        {
            FirstName = dFirstName;
            LastName = dLastName;
        }
    }
}

XAML

<Window x:Class="DataGridExampleSelfTry.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:DataGridExampleSelfTry"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="450">
    <Window.DataContext>
        <local:MainWindowVM/>
    </Window.DataContext>
    <StackPanel>
        <DataGrid x:Name="XAML_DataGrid"  
                            AutoGenerateColumns="False"  CanUserAddRows="False"                               
                            ItemsSource="{Binding animelistforbinding}" Margin="5" 
                            CanUserSortColumns="False" HorizontalGridLinesBrush="Gray" 
                            VerticalGridLinesBrush="Gray" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding FirstName, NotifyOnTargetUpdated=True, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                                    Header="First name" Width="*" IsReadOnly="False"/>
                <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" Width="*" IsReadOnly="False"/>
               
            </DataGrid.Columns>
        </DataGrid>
        <StackPanel Orientation="Horizontal">
            <Label Content="The First Cell Value is : "/>
            <Label Content="{ Binding FirstCell}"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Label Content="The Second Cell Value is : "/>
            <Label Content="{ Binding SecondCell}"/>
        </StackPanel>
          <Button Content="Button" Margin="50"/>
    </StackPanel>
   
</Window>

感谢您的帮助。

【问题讨论】:

    标签: wpf binding datagrid wpfdatagrid inotifypropertychanged


    【解决方案1】:

    要么直接绑定到DataGrid 的第一列绑定到的同一属性:

    <Label Content="{Binding animelistforbinding[0].FirstName}"/>
    

    ...或在设置animelistforbinding 中第一项的FirstName 属性时设置FirstCell 属性。您可以通过为视图模型中的第一项处理 PropertyChanged 事件来做到这一点:

    public MainWindowVM()
    {
        addinganime.Add(new animies("Monkey", "D Luffy"));
    
        animelistforbinding = addinganime;
    
        FirstCell = animelistforbinding[0].FirstName;
        SecondCell = animelistforbinding[0].LastName;
        animelistforbinding[0].PropertyChanged += (s, e) => FirstCell = animelistforbinding[0].FirstName;
    }
    

    【讨论】:

    • 我无法使用 XAML(第一个解决方案)。但是第二种解决方案对我有用。谢谢。
    • @mechbaral:抱歉,我给你的绑定路径错误。查看我的编辑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-19
    • 2012-08-17
    相关资源
    最近更新 更多