UI
<Grid x:Class="WzlyTool.ReplyContentUI" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="497" Width="771" xmlns:my="clr-namespace:WzlyTool"> <Grid.Resources> <Style TargetType="ListBox" x:Key="listboxStyle"> <Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter> </Style> <Style TargetType="ListBoxItem"> <Style.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGray"></SolidColorBrush> <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGray"></SolidColorBrush> </Style.Resources> </Style> </Grid.Resources> <Grid> <RichTextBox Foreground="#ffeeeeee" Name="txtLog" Margin="46,254,7,35" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <!--设置行间距--> <RichTextBox.Document> <FlowDocument Focusable="True" LineHeight="2"> </FlowDocument> </RichTextBox.Document> </RichTextBox> <ListBox Name="listBox" SelectionMode="Single" Style="{StaticResource listboxStyle}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Button Tag="{Binding id}" Name="btnEditItem" Click="btnEditItem_Click" Margin="1,1,12,1">Edit</Button> <TextBlock TextWrapping="Wrap"/> <CheckBox Width="300" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Foreground="{Binding color}" VerticalAlignment="Center" Content="{Binding msg}" IsChecked="{Binding isChecked}"> <CheckBox.Resources> <Style TargetType="TextBlock"> <Setter Property="TextWrapping" Value="Wrap" /> </Style> </CheckBox.Resources> </CheckBox> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Button Name="btnSaveConfig" Content="Save config content" Margin="474,28,0,0" Height="39" VerticalAlignment="Top" BorderBrush="#FFE2E2E2" Background="Gray" BorderThickness="2" Foreground="White" Click="btnSaveConfig_Click" HorizontalAlignment="Left" Width="154"> </Button> <Button Name="btnAdd" Content="+ add content item" Margin="196,28,0,0" Height="39" VerticalAlignment="Top" BorderBrush="#FFE2E2E2" Background="Gray" BorderThickness="2" Foreground="White" Click="btnAdd_Click" HorizontalAlignment="Left" Width="154"> </Button> </Grid> </Grid>
the ui class behinde code :
using System; using System.Collections.Generic; using System.Linq; using System.Text; 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.Shapes; using System.Data; using System.Net; using System.Security.Cryptography.X509Certificates; using System.IO; using System.Net.Security; using System.Runtime.Serialization.Formatters.Binary; namespace WzlyTool { /// <summary> /// Interaction logic for ReplyWind.xaml /// </summary> public partial class ReplyContentUI : Grid { public ReplyContentUI() { InitializeComponent(); Loaded += new RoutedEventHandler(ReplyContentUI_Loaded); listContent = new List<ReplyContentItem>(); listBox.DataContext = listContent; listBox.ItemsSource = listContent; } private string file = "reMsg.d"; public List<ReplyContentItem> listContent; private SolidColorBrush checkedColor = new SolidColorBrush(Colors.Green); private SolidColorBrush unCheckedColor = new SolidColorBrush(Colors.Gray); private void btnAdd_Click(object sender, RoutedEventArgs e) { ReplyContentItem c = new ReplyContentItem() { color = "Green", isChecked = true, msg = "hello" + DateTime.Now }; listBox.ItemsSource = null; listContent.Add(c); listBox.ItemsSource = listContent; } void ReplyContentUI_Loaded(object sender, RoutedEventArgs e) { listContent.Add(new ReplyContentItem() { color = "Red", id = Guid.NewGuid().ToString(), isChecked = true, msg = "yuiyui8888888888888yui" }); listContent.Add(new ReplyContentItem() { color = "Gray", id = Guid.NewGuid().ToString(), isChecked = true, msg = "yuiyuiyui" }); listBox.ItemsSource = null; testLoadConfigContent(); listBox.ItemsSource = listContent; } private void btnSaveConfig_Click(object sender, RoutedEventArgs e) { testSaveConfigContent(); } private void CheckBox_Checked(object sender, RoutedEventArgs e) { if (((CheckBox)sender).IsChecked == true) { ((CheckBox)sender).Foreground = checkedColor; } else { ((CheckBox)sender).Foreground = unCheckedColor; } } private void CheckBox_Unchecked(object sender, RoutedEventArgs e) { if (((CheckBox)sender).IsChecked == true) { ((CheckBox)sender).Foreground = checkedColor; } else { ((CheckBox)sender).Foreground = unCheckedColor; } } void displayLog(string txt) { txtLog.AppendText("" + DateTime.Now + ":" + txt + "\r\n"); txtLog.ScrollToEnd(); } private void btnEditItem_Click(object sender, RoutedEventArgs e) { string id = "" + ((Button)sender).Tag; ReplyContentItem c = listContent.FirstOrDefault(n => n.id == id); if (c != null) { c.msg = "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"; listBox.ItemsSource = null; listBox.ItemsSource = listContent; // MessageBox.Show(c.msg ); } } void testLoadConfigContent() { try { if (!File.Exists(file)) { return; } List<ReplyContentItem> obj; // 打开文件,并进行反序列化得到对象 Stream stream = File.Open(file, FileMode.Open); BinaryFormatter formatter = new BinaryFormatter(); obj = (List<ReplyContentItem>)formatter.Deserialize(stream); stream.Close(); listBox.ItemsSource = null; listContent.Clear(); foreach (ReplyContentItem m in obj) { if ("" + m.color == "") { m.color = "Green"; } if (m.isChecked) { m.color = "Green"; } else { m.color = "Gray"; } listContent.Add(m); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void testSaveConfigContent() { int cnt = listBox.Items.Count;//listView 是UI上的列表控件 List<ReplyContentItem> obj = new List<ReplyContentItem>(); for (int i = 0; i < cnt; i++) { // obj.Add (listBox.Items[i] as ReplyContentItem) ;/// new Msg() { isChecked = listView.Items[i] as , word = "test" + i }; obj.Add(listBox.Items[i] as ReplyContentItem);/// new Msg() { isChecked = listView.Items[i] as , word = "test" + i }; } // 创建一个文件,并将对象序列化后存储在其中 Stream stream = File.Open(file, FileMode.Create); BinaryFormatter formatter = new BinaryFormatter(); //BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, obj); stream.Close(); // 将对象置空 obj = null; } } [Serializable] public class ReplyContentItem { public ReplyContentItem() { color = "Gray"; id = Guid.NewGuid().ToString(); } public string id { get; set; } public string msg { get; set; } public bool isChecked { get; set; } public string color { get; set; } } }
another article:WPF C# 序列化保存设置 https://www.cnblogs.com/wgscd/articles/8630128.html