There is a DataGrid . There are problems with uploading content to XML.
Class to serialize
[Serializable] public class Buttons { public bool VisibilityBut { get; set; } public string Name { get; set; } public string PathImag { get; set; } public string PathExe { get; set; } } If I do serialization like this
XmlSerializer formatter = new XmlSerializer(typeof(Buttons)); using (FileStream fs = new FileStream(@"XML.XML", FileMode.OpenOrCreate)) formatter.Serialize(fs, DataGrid1); then the file is created empty and an exception is thrown
System.InvalidOperationException: "Error creating XML document." InvalidCastException`: Failed to cast the object type "System.Windows.Controls.DataGrid" to the type of "Button Panel. Buttons".
And if I do this:
if (DataGrid1.ItemsSource == null) return; var serializer = new XmlSerializer(typeof(List<Buttons>)); var writer = new StreamWriter("XML.xml"); serializer.Serialize(writer, DataGrid1.ItemsSource as List<Buttons>); writer.Close(); then only the XML header is created in the file, and everything else is empty, by selecting the types, the solution is this.
ObservableCollection<ButtonEIIS> collection = null; private void butSaveXML_Click(object sender, RoutedEventArgs e) // Π½Π°ΠΆΠ°Π»ΠΈ ΠΊΠ½. "Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ Π² XML" { var serializer = new XmlSerializer(typeof(ObservableCollection<Buttons>)); using (FileStream fs = new FileStream(@"XML.XML", FileMode.OpenOrCreate)) { serializer.Serialize(fs, DataGrid1.ItemsSource as ObservableCollection<Buttons>); } } in XAML binding such
<DataGrid x:Name="DataGrid1" Margin="10,171,10,52" FontSize="14" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridCheckBoxColumn Header="ΠΠΈΠ΄" Binding="{Binding VisBut}"/> <DataGridTextColumn Header="ΠΠ°Π΄ΠΏΠΈΡΡ Π½Π° ΠΊΠ½ΠΎΠΏΠΊΠ΅ (ΠΏΠ°ΠΏΠΊΠ° ΠΏΠΎΠ΄ΡΠΈΡΡΠ΅ΠΌΡ)" Binding="{Binding NameSubEIIS}"/> <DataGridTextColumn Header="ΠΠΎΠ»Π½ΡΠΉ ΠΏΡΡΡ ΠΊ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ΅" Binding="{Binding PathPNG}"/> <DataGridTextColumn Header="ΠΠΎΠ»Π½ΡΠΉ ΠΏΡΡΡ ΠΊ Π·Π°ΠΏΡΡΠΊΠ°Π΅ΠΌΠΎΠΌΡ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΡ" Binding="{Binding PathEXE}"/> </DataGrid.Columns> </DataGrid>
List<Buttons>? - Andrei NOPusing, as you did in the first case. Also, the error in the first case draws your attention to the fact that you had to passDataGrid1.ItemsSourceinstead of justDataGrid1, andtypeof(List<Buttons>)instead oftypeof(Buttons). - Vadim OvchinnikovDataGrid1.ItemsSource as List<Buttons>- Andrey NOP