WPF Datagrid 到 XML [关闭]

Posted

技术标签:

【中文标题】WPF Datagrid 到 XML [关闭]【英文标题】:WPF Datagrid to XML [closed] 【发布时间】:2021-02-12 08:28:02 【问题描述】:

我需要一个实际的帮助。我正在尝试在 wpf 中编写用户可编辑的数据网格。我已经完成了大部分工作,但我坚持将数据网格保存到 xml 并将其从 xml 加载到 wtf。

我会在我的代码上留下一些空白点,用“帮助评论”和一些关于我成功做的事情的 cmets。考虑到我之前没有 c# 和 wpf 的知识,我为自己的成就感到非常自豪。可悲的是,我也是 xml 的新手,对于我的具体问题没有好的教程材料。

除了保存和加载按钮之外,所有按钮都可以使用。

这是 XAML 文件

<Window x:Class="WpfAppLunes.MainWindow" <!-- This changes according to project name -->
    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:WpfAppLunes" <!-- This changes according to project name -->
    mc:Ignorable="d" FontSize="18"
    Title="WPF_Lunes" Height="500" Width="800">



<Border Padding="10">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Row 0 -->
        <!-- Title of the Application -->
        <TextBlock x:Name="BigHeader" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" TextWrapping="Wrap" Margin="1" FontSize="36" FontWeight="Bold" Text="Monthly Transactions" />

        <DataGrid Grid.Row="1"  AutoGenerateColumns="False" CanUserAddRows="False" x:Name="dgContent">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Transaction Name" Binding="Binding transactionName"/>
                <DataGridTextColumn Header="Amount '$'" Binding="Binding Amount"/>
            </DataGrid.Columns>
        </DataGrid>
        <!--
        <ListView Margin="10" Name="lvUsers" Grid.Row="1" Grid.ColumnSpan="2">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Transaction Name"  DisplayMemberBinding="Binding transactionName" />
                    <GridViewColumn Header="Amount '$'" DisplayMemberBinding="Binding Amount" />
                </GridView>
            </ListView.View>
        </ListView>
        -->

        <TextBlock x:Name="TransactionHeader" Grid.Row="3" Grid.Column="0" TextWrapping="Wrap" Margin="1" Text="Transaction Name" />
        <TextBlock x:Name="AmountHeader" Grid.Row="3" Grid.Column="1" TextWrapping="Wrap" Margin="1" Text="Amount" />
        <!-- Row 3 -->
        <!-- Text Boxes for the user to declare next item and its price -->
        <TextBox x:Name="newTransaction" Grid.Row="4" Grid.Column="0" Padding="5" Margin="5"/>
        <TextBox x:Name="newAmount" Grid.Row="4" Grid.Column="1" Padding="5" Margin="5" PreviewTextInput="newAmount_PreviewTextInput"/>
        <ComboBox Grid.Row="4" Grid.Column="2" Name="Language" Width="170" Margin="1" VerticalContentAlignment="Center" VerticalAlignment="Top" MaxDropDownHeight="auto"></ComboBox>
        <!-- <TextBox Text="Binding Path=value, StringFormat=0:#.##" /> -->
        
        <!-- Row 4 -->
        <!-- Buttons to Add to the list or delete from the list -->
        <Button x:Name="addButton" Click="addButton_Click" Grid.Row="5" Grid.Column="0" Margin="10" Content="Add" />
        <Button x:Name="deleteButton" Click="deleteButton_Click" Grid.Row="5" Grid.Column="1" Margin="10" Content="Delete" />
        <Button x:Name="changeButton" Content="Dil Değiştir" Margin="10" Grid.Column="3" Grid.Row="5" Click="changeButton_Click"/>
        <!-- Row 5 -->
        <!-- Save or Load the file -->
        <Button x:Name="saveButton" Click="saveButton_Click" Grid.Row="6" Grid.Column="0" Margin="10" Content="Save" />
        <Button x:Name="loadButton" Click="loadButton_Click" Grid.Row="6" Grid.Column="1" Margin="10" Content="Load" />
   
        <!--Row 7 test for localization -->
        
    </Grid>
</Border>

这是 c# 文件。

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
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 Finisar.SQLite;
using System.Xml;
using System.Xml.Linq;
using System.IO;
//I tried to use a lot of stuffs and did not bother to remove them. If you add another, put a comment
//next to it please.
namespace WpfAppLunes

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    
//I initialize my datagrid here.
        ObservableCollection<transaction> myObjects;

        public MainWindow()
        
            InitializeComponent();

            myObjects = new ObservableCollection<transaction>()
            
                new transaction()transactionName = "Salary", Amount = "$1,670.00",
                new transaction()transactionName = "Car", Amount = "-$60.00",
                new transaction()transactionName = "Clothing", Amount = "-$320.00",
                new transaction()transactionName = "Food", Amount = "-$85.00",
                new transaction()transactionName = "Leisure", Amount = "-$35.00",
                new transaction()transactionName = "Living", Amount = "-$560.00"
            ;

            this.dgContent.ItemsSource = myObjects;

            BindLanguage();

            
        


//This is my combobox that lets the user choose from language settings
        #region Bind Language From and To Combobox
        private void BindLanguage()
        
            DataTable dtCurrency = new DataTable();

            //Add display column in DataTable
            dtCurrency.Columns.Add("Text");

            //Add value column in DataTable
            dtCurrency.Columns.Add("Value");

            dtCurrency.Rows.Add("EN", 0);
            dtCurrency.Rows.Add("TR", 1);

            Language.ItemsSource = dtCurrency.DefaultView;
            Language.DisplayMemberPath = "Text";
            Language.SelectedValuePath = "Value";
            Language.SelectedIndex = 0;
        
        #endregion

//It adds the transaction name and value to the data grid. It also formats the datagrid amount view.
        //Add button properties
        private void addButton_Click(object sender, RoutedEventArgs e)
        
            double amountFormat;

            amountFormat = double.Parse(this.newAmount.Text);
            if (amountFormat < 0)
            
                amountFormat *= -1;
                transaction myObject = new transaction()  transactionName = this.newTransaction.Text, Amount = "-$" + amountFormat.ToString("N2") ;
                myObjects.Add(myObject);
            
            else
            
                transaction myObject = new transaction()  transactionName = this.newTransaction.Text, Amount = "$" + amountFormat.ToString("N2") ;
                myObjects.Add(myObject);
            
        

//It deletes the selected value from the data grid.
        //Delete button properties
        private void deleteButton_Click(object sender, RoutedEventArgs e)
        
            if (dgContent.SelectedIndex >= 0)
            
                //remove the selectedItem from the collection source
                transaction selected = dgContent.SelectedItem as transaction;
                myObjects.Remove(selected);
            
        

        
        //Save button properties
        private void saveButton_Click(object sender, RoutedEventArgs e)
        
/* HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP */            
        

        //Load button properties
//I thought it would be good idea to clear the datagrid before I actually load the file from xml
//I think it will be good idea to create a new button just to clear the datagrid. I will add it later.
        private void loadButton_Click(object sender, RoutedEventArgs e)
        
/* HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP HELP */
            myObjects.Clear();

        

        //It gets the textbox values.
        public class transaction
        
            public string transactionName  get; set; 
            public string Amount  get; set; 
        

        //Makes sure that user can input only the +/- decimal values.
        private void newAmount_PreviewTextInput(object sender, TextCompositionEventArgs e)
        
            //Because this line and next line comes before regex, they can be inputted.
            //It will allow the user to add negative value
            if (e.Text.Equals("-") && newAmount.Text.Length.Equals(0))
                return;
            //It will allow the user input 1 decimal point.
            //User can input more than 2 numbers after the decimal point to my displeasure
            //I did manage to get a workaround on this though. When the amount is added to the datagrid
            //it will show in the way I want it to show.
            if (e.Text.Equals(".") && !newAmount.Text.Contains("."))
                return;

            //This makes sure that user cannot enter anything other than number.
            Regex regex = new Regex("[^0-9]+");
            e.Handled = regex.IsMatch(e.Text);
            
        
//It is my localization method.       
//This is my version of localization. I could not get xaml change language because it would not recognize 
//loc type variables for some reason. Maybe I missed some library. :D 
//I would love it if someone told me the possible problem, but it is my last concern.
        private void changeButton_Click(object sender, RoutedEventArgs e)
        
            if (Language.Text == "EN" && changeButton.Content.ToString() == "Change Language")
            
                changeButton.Content = "Dil Değiştir";
                addButton.Content = "Add";
                deleteButton.Content = "Delete";
                saveButton.Content = "Save";
                loadButton.Content = "Load";
                TransactionHeader.Text = "Transaction Name";
                AmountHeader.Text = "Amount";
                dgContent.Columns[0].Header = "Transaction Name";
                dgContent.Columns[1].Header = "Amount '$'";
                BigHeader.Text = "Monthly Transactions";
            
            if(Language.Text == "TR" && changeButton.Content.ToString() == "Dil Değiştir")
            
                changeButton.Content = "Change Language";
                addButton.Content = "Ekle";
                deleteButton.Content = "Sil";
                saveButton.Content = "Kaydet";
                loadButton.Content = "Yükle";
                TransactionHeader.Text = "İşlem";
                AmountHeader.Text = "İşlem Miktarı";
                dgContent.Columns[0].Header = "İşlem";
                dgContent.Columns[1].Header = "İşlem Miktarı '$'";
                BigHeader.Text = "Aylık Gelir/Giderler";
            
        
       
    

---代码结束--- 我已经删除了我尝试实现的代码并将它们注释掉。我很害羞。

我真的很感激它是在我的 cmets 上实现或可以实现的实际代码。我已经在网上看了好几个小时了。我快要被挫折发疯了。

【问题讨论】:

【参考方案1】:

为了从 XML 序列化/反序列化对象,您必须使用 XmlSerializer 类。 我不知道你必须使用的 XML 格式,所以我想你不会介意的。

此代码将从名为 mydata.xml 的桌面文件保存/加载数据

    //Save button properties
    private void saveButton_Click(object sender, RoutedEventArgs e)
    
        var fileName = GetSaveFilePath();
        var serializer = new XmlSerializer(typeof(List<transaction>));
        using (TextWriter stream = new StreamWriter(fileName))
        
            serializer.Serialize(stream, myObjects.ToList());
            stream.Flush();
        
    


    /// <summary>
    /// Where you want to save data
    /// </summary>
    private string GetSaveFilePath()
    
        var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        var fileName = System.IO.Path.Combine(desktop, "myData.xml");
        return fileName;
    

    //Load button properties
    private void loadButton_Click(object sender, RoutedEventArgs e)
    
        myObjects.Clear();

        var fileName = GetSaveFilePath();
        var serializer = new XmlSerializer(typeof(List<transaction>));
        using (TextReader stream = new StreamReader(fileName))
        
            var list = (List<transaction>)serializer.Deserialize(stream);
            foreach (var item in list)
            
                myObjects.Add(item);
            
        
    

【讨论】:

这实际上解决了我的问题。感谢您,我的代码可以正常工作并完成我想要它做的所有事情。现在,我将不得不为未来的项目研究它。我处理了文件位置和名称。 你能告诉我,它的哪一部分删除了旧的xml文件来创建一个新的xml文件? 删除部分丢失:我用来编写新 XML 的流替换了文件的旧内容。所以不需要删除现有文件

以上是关于WPF Datagrid 到 XML [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

找不到将Wpf DataGrid列绑定到属性的方法[关闭]

WPF DataGrid 绑定到 XML 未更新

WPF关闭datagrid插入行

在运行时将新表重新加载/替换到 WPF DataGrid 中

在 WPF DataGrid 中使用多个 XML 文档

WPF Toolkit Datagrid - 如何关闭选择?