none
求教,属性更新后,在datagrid控件中不显示。 RRS feed

  • 问题

  • 大家好:

    我最近编写了一个软件,具体功能是:

    读取文件夹中的所有文件并改名,并想在datagrid控件中显示更改情况,主要功能都已实现,但是在最后一栏“是否更改”(绑定自定义类FileInfoItem的IsChanged属性)中,无论我怎么操作,datagrid控件中均毫无反应。

    前台使用WPF,后台使用C#,还请大家指点!

    前台代码如下:

    <Window x:Class="FileNamesReadAndChange.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:FileNamesReadAndChange"
            mc:Ignorable="d"
            Title="MainWindow" Height="450" Width="800">
        <Grid>
            <DataGrid x:Name="dg_List" HorizontalAlignment="Left" Height="399" Margin="370,10,0,0" VerticalAlignment="Top" Width="412" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Path=ID,Mode=TwoWay}" ClipboardContentBinding="{x:Null}" Header="序号"/>
                    <DataGridTextColumn Binding="{Binding Path=FileName,Mode=TwoWay}" ClipboardContentBinding="{x:Null}" Header="现文件名" IsReadOnly="True"/>
                    <DataGridTextColumn Binding="{Binding Path=FileName_dest,Mode=TwoWay}" ClipboardContentBinding="{x:Null}" Header="将改文件名"/>
                    <DataGridTextColumn Binding="{Binding Path=IsChanged,Mode=TwoWay}" ClipboardContentBinding="{x:Null}" Header="是否更改"/>
                </DataGrid.Columns>
            </DataGrid>
            <Button x:Name="bt_ReadFilesName" Content="读取文件名" HorizontalAlignment="Left" VerticalAlignment="Top" Width="128" Margin="10,351,0,0" Height="58" Click="Bt_ReadFilesName_Click"/>
            <Button x:Name="bt_WriteFilesName" Content="保存文件名" HorizontalAlignment="Left" VerticalAlignment="Top" Width="123" Margin="172,351,0,0" Height="58" Click="Bt_WriteFilesName_Click"/>
            <GroupBox Header="测试信息栏" HorizontalAlignment="Left" Height="74" Margin="10,10,0,0" VerticalAlignment="Top" Width="316">
                <StackPanel Margin="0">
                    <Label x:Name="test_lb" Content="测试信息" HorizontalAlignment="Left" VerticalAlignment="Top"/>
                    <TextBox x:Name="test_TextBox" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="294" Margin="0" IsReadOnly="True"/>
                </StackPanel>
            </GroupBox>
        </Grid>
    </Window>

    后台代码如下:

    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;
    
    using System.Windows.Forms;
    using System.IO;
    using System.Collections.ObjectModel;
    namespace FileNamesReadAndChange
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                dg_List.ItemsSource = fInfos_dg;
            }
            FolderBrowserDialog fbd;
            string folderPath;
            ObservableCollection<FileInfoItem> fInfos_dg= new ObservableCollection<FileInfoItem>();
            DirectoryInfo directoryInfo;
            FileInfo[] fileInfos;
    
            private void Bt_ReadFilesName_Click(object sender, RoutedEventArgs e)
            {
                fInfos_dg.Clear();
                fbd = new FolderBrowserDialog();
                DialogResult dialogResult = fbd.ShowDialog();
                if (dialogResult == System.Windows.Forms.DialogResult.OK)
                {
                    if (fbd.SelectedPath != null)
                    {
                        test_lb.Content = "选择文件夹路径";
                        folderPath = fbd.SelectedPath;
                        test_TextBox.Text = folderPath;                  
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    return;
                }
                directoryInfo = new DirectoryInfo(folderPath);
                fileInfos = directoryInfo.GetFiles();
                int i = 1;        
                foreach(FileInfo fileInfo in fileInfos)
                {
                    fInfos_dg.Add(new FileInfoItem()
                    {
                        ID = i++,
                        FileName = fileInfo.Name,
                        FileName_dest = fileInfo.Name,
                    });
                }
            }
    
            private void Bt_WriteFilesName_Click(object sender, RoutedEventArgs e)
            {
                if (fInfos_dg != null)
                {
                    string srcPath, destPath;
                    foreach (FileInfo finfo_w in fileInfos)
                    {
                        foreach (FileInfoItem item in fInfos_dg)
                        {
                            if ((finfo_w.Name == item.FileName) && (item.FileName != item.FileName_dest))
                            {
                                item.IsChanged = true;
                                srcPath = System.IO.Path.Combine(folderPath, finfo_w.Name);
                                destPath = System.IO.Path.Combine(folderPath, item.FileName_dest);
                                finfo_w.MoveTo(destPath);
                            }
                        }
                    }
                    System.Windows.MessageBox.Show("所有文件已改名!");
                }
                else
                {
                    System.Windows.MessageBox.Show("要先读取文件信息");
                    return;
                }
            }
        }
        public class FileInfoItem
        {
            public int ID { get; set; }
            public string FileName { get; set; }
            public string FileName_dest { get; set; }
            public bool IsChanged { get; set; } 
        }
    }
    

    2019年9月8日 2:23

答案

  • 请在数据模型上使用INotifyPropertyChanged接口

    修改如下

    public class FileInfoItem:INotifyPropertyChanged
        {
            public int ID { get; set; }
            public string FileName { get; set; }
            public string FileName_dest { get; set; }
            private bool b;
            public bool IsChanged { get=>b; set { b = value;onchanged(new PropertyChangedEventArgs("IsChanged")); } }
            protected void onchanged(PropertyChangedEventArgs arg) => PropertyChanged?.Invoke(this, arg);
            public event PropertyChangedEventHandler PropertyChanged;
        }


    • 已编辑 ARM830 2019年9月8日 5:22
    • 已标记为答案 wxysy 2019年9月8日 14:23
    2019年9月8日 5:19

全部回复

  • 请在数据模型上使用INotifyPropertyChanged接口

    修改如下

    public class FileInfoItem:INotifyPropertyChanged
        {
            public int ID { get; set; }
            public string FileName { get; set; }
            public string FileName_dest { get; set; }
            private bool b;
            public bool IsChanged { get=>b; set { b = value;onchanged(new PropertyChangedEventArgs("IsChanged")); } }
            protected void onchanged(PropertyChangedEventArgs arg) => PropertyChanged?.Invoke(this, arg);
            public event PropertyChangedEventHandler PropertyChanged;
        }


    • 已编辑 ARM830 2019年9月8日 5:22
    • 已标记为答案 wxysy 2019年9月8日 14:23
    2019年9月8日 5:19
  • 一针见血,感谢您的耐心回复!

    2019年9月8日 14:25