UI

  1. <Grid x:Class="WzlyTool.ReplyContentUI"
  2.  
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. Height="497" Width="771" xmlns:my="clr-namespace:WzlyTool">
  6.  
  7. <Grid.Resources>
  8. <Style TargetType="ListBox" x:Key="listboxStyle">
  9. <Setter Property="FocusVisualStyle" Value="{x:Null}"></Setter>
  10.  
  11. </Style>
  12. <Style TargetType="ListBoxItem">
  13. <Style.Resources>
  14. <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGray"></SolidColorBrush>
  15. <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="LightGray"></SolidColorBrush>
  16. </Style.Resources>
  17. </Style>
  18. </Grid.Resources>
  19.  
  20. <Grid>
  21.  
  22. <RichTextBox Foreground="#ffeeeeee" Name="txtLog" Margin="46,254,7,35" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
  23. <!--设置行间距-->
  24. <RichTextBox.Document>
  25. <FlowDocument Focusable="True" LineHeight="2">
  26. </FlowDocument>
  27. </RichTextBox.Document>
  28. </RichTextBox>
  29.  
  30. <ListBox Name="listBox" SelectionMode="Single" Style="{StaticResource listboxStyle}" >
  31. <ListBox.ItemTemplate>
  32. <DataTemplate>
  33. <StackPanel Orientation="Horizontal">
  34.  
  35. <Button Tag="{Binding id}" Name="btnEditItem" Click="btnEditItem_Click" Margin="1,1,12,1">Edit</Button>
  36. <TextBlock TextWrapping="Wrap"/>
  37. <CheckBox Width="300" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Foreground="{Binding color}"
  38. VerticalAlignment="Center" Content="{Binding msg}" IsChecked="{Binding isChecked}">
  39.  
  40. <CheckBox.Resources>
  41. <Style TargetType="TextBlock">
  42. <Setter Property="TextWrapping" Value="Wrap" />
  43. </Style>
  44. </CheckBox.Resources>
  45. </CheckBox>
  46.  
  47. </StackPanel>
  48.  
  49. </DataTemplate>
  50. </ListBox.ItemTemplate>
  51. </ListBox>
  52.  
  53. <Button Name="btnSaveConfig"
  54. Content="Save config content" Margin="474,28,0,0" Height="39"
  55. VerticalAlignment="Top"
  56. BorderBrush="#FFE2E2E2"
  57. Background="Gray"
  58. BorderThickness="2"
  59. Foreground="White" Click="btnSaveConfig_Click" HorizontalAlignment="Left" Width="154">
  60.  
  61. </Button>
  62.  
  63. <Button Name="btnAdd"
  64. Content="+ add content item" Margin="196,28,0,0" Height="39"
  65. VerticalAlignment="Top"
  66. BorderBrush="#FFE2E2E2"
  67. Background="Gray"
  68. BorderThickness="2"
  69. Foreground="White" Click="btnAdd_Click" HorizontalAlignment="Left" Width="154">
  70.  
  71. </Button>
  72.  
  73. </Grid>
  74.  
  75. </Grid>

  

the ui class behinde code :

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Shapes;
  13. using System.Data;
  14. using System.Net;
  15. using System.Security.Cryptography.X509Certificates;
  16. using System.IO;
  17. using System.Net.Security;
  18. using System.Runtime.Serialization.Formatters.Binary;
  19.  
  20. namespace WzlyTool
  21. {
  22. /// <summary>
  23. /// Interaction logic for ReplyWind.xaml
  24. /// </summary>
  25. public partial class ReplyContentUI : Grid
  26. {
  27. public ReplyContentUI()
  28. {
  29. InitializeComponent();
  30. Loaded += new RoutedEventHandler(ReplyContentUI_Loaded);
  31. listContent = new List<ReplyContentItem>();
  32. listBox.DataContext = listContent;
  33. listBox.ItemsSource = listContent;
  34.  
  35. }
  36. private string file = "reMsg.d";
  37. public List<ReplyContentItem> listContent;
  38. private SolidColorBrush checkedColor = new SolidColorBrush(Colors.Green);
  39. private SolidColorBrush unCheckedColor = new SolidColorBrush(Colors.Gray);
  40. private void btnAdd_Click(object sender, RoutedEventArgs e)
  41. {
  42. ReplyContentItem c = new ReplyContentItem() { color = "Green", isChecked = true, msg = "hello" + DateTime.Now };
  43. listBox.ItemsSource = null;
  44. listContent.Add(c);
  45. listBox.ItemsSource = listContent;
  46.  
  47. }
  48.  
  49. void ReplyContentUI_Loaded(object sender, RoutedEventArgs e)
  50. {
  51. listContent.Add(new ReplyContentItem() { color = "Red", id = Guid.NewGuid().ToString(), isChecked = true, msg = "yuiyui8888888888888yui" });
  52. listContent.Add(new ReplyContentItem() { color = "Gray", id = Guid.NewGuid().ToString(), isChecked = true, msg = "yuiyuiyui" });
  53. listBox.ItemsSource = null;
  54. testLoadConfigContent();
  55. listBox.ItemsSource = listContent;
  56.  
  57. }
  58.  
  59. private void btnSaveConfig_Click(object sender, RoutedEventArgs e)
  60. {
  61. testSaveConfigContent();
  62. }
  63.  
  64. private void CheckBox_Checked(object sender, RoutedEventArgs e)
  65. {
  66.  
  67. if (((CheckBox)sender).IsChecked == true)
  68. {
  69. ((CheckBox)sender).Foreground = checkedColor;
  70. }
  71. else
  72. {
  73. ((CheckBox)sender).Foreground = unCheckedColor;
  74. }
  75.  
  76. }
  77.  
  78. private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
  79. {
  80. if (((CheckBox)sender).IsChecked == true)
  81. {
  82. ((CheckBox)sender).Foreground = checkedColor;
  83. }
  84. else
  85. {
  86. ((CheckBox)sender).Foreground = unCheckedColor;
  87. }
  88.  
  89. }
  90.  
  91. void displayLog(string txt)
  92. {
  93. txtLog.AppendText("" + DateTime.Now + ":" + txt + "\r\n");
  94. txtLog.ScrollToEnd();
  95. }
  96.  
  97. private void btnEditItem_Click(object sender, RoutedEventArgs e)
  98. {
  99. string id = "" + ((Button)sender).Tag;
  100. ReplyContentItem c = listContent.FirstOrDefault(n => n.id == id);
  101. if (c != null)
  102. {
  103.  
  104. c.msg = "gggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg";
  105. listBox.ItemsSource = null;
  106. listBox.ItemsSource = listContent;
  107. // MessageBox.Show(c.msg );
  108.  
  109. }
  110.  
  111. }
  112.  
  113. void testLoadConfigContent()
  114. {
  115.  
  116. try
  117. {
  118.  
  119. if (!File.Exists(file)) { return; }
  120. List<ReplyContentItem> obj;
  121. // 打开文件,并进行反序列化得到对象
  122. Stream stream = File.Open(file, FileMode.Open);
  123. BinaryFormatter formatter = new BinaryFormatter();
  124. obj = (List<ReplyContentItem>)formatter.Deserialize(stream);
  125. stream.Close();
  126. listBox.ItemsSource = null;
  127. listContent.Clear();
  128. foreach (ReplyContentItem m in obj)
  129. {
  130. if ("" + m.color == "") { m.color = "Green"; }
  131. if (m.isChecked)
  132. {
  133. m.color = "Green";
  134. }
  135. else
  136. {
  137. m.color = "Gray";
  138. }
  139. listContent.Add(m);
  140. }
  141.  
  142. }
  143. catch (Exception ex)
  144. {
  145. MessageBox.Show(ex.Message);
  146. }
  147.  
  148. }
  149. public void testSaveConfigContent()
  150. {
  151.  
  152. int cnt = listBox.Items.Count;//listView 是UI上的列表控件
  153. List<ReplyContentItem> obj = new List<ReplyContentItem>();
  154. for (int i = 0; i < cnt; i++)
  155. {
  156.  
  157. // obj.Add (listBox.Items[i] as ReplyContentItem) ;/// new Msg() { isChecked = listView.Items[i] as , word = "test" + i };
  158. obj.Add(listBox.Items[i] as ReplyContentItem);/// new Msg() { isChecked = listView.Items[i] as , word = "test" + i };
  159.  
  160. }
  161. // 创建一个文件,并将对象序列化后存储在其中
  162. Stream stream = File.Open(file, FileMode.Create);
  163. BinaryFormatter formatter = new BinaryFormatter();
  164. //BinaryFormatter formatter = new BinaryFormatter();
  165. formatter.Serialize(stream, obj);
  166. stream.Close();
  167. // 将对象置空
  168. obj = null;
  169. }
  170.  
  171. }
  172.  
  173. [Serializable]
  174. public class ReplyContentItem
  175. {
  176. public ReplyContentItem()
  177. {
  178. color = "Gray";
  179. id = Guid.NewGuid().ToString();
  180. }
  181. public string id { get; set; }
  182. public string msg { get; set; }
  183. public bool isChecked { get; set; }
  184.  
  185. public string color
  186. {
  187. get;
  188. set;
  189. }
  190.  
  191. }
  192.  
  193. }

  

another article:WPF C# 序列化保存设置    https://www.cnblogs.com/wgscd/articles/8630128.html

WPF save listbox config的更多相关文章

  1. WPF ItemsControl ListBox ListView比较

    在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...

  2. WPF中ListBox的项ListBoxItem被选中的时候Background变化

    使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:

  3. Android Kernel save defalut config

    /********************************************************************************* * Android Kernel ...

  4. WPF中ListBox滚动时的缓动效果

    原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...

  5. [WPF系列]-ListBox

    引言 本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义.   Listbox平滑滚动 <ListBox Ite ...

  6. WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题

    最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...

  7. WPF的ListBox中的RadioButton不能单选问题

    WPF不知道是微软故意弄的还是真的匆忙的推出的产品,在实际开发过程中有很多小问题解决很麻烦. 今天主要说一下ListBox中使用RadioButton的时候里面的RadioButton不能单选!居然成 ...

  8. WPF中ListBox /ListView如何改变选中条背景颜色

    适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html

  9. 【WPF】ListBox嵌套与事件冒泡

    问题:两个ListBox嵌套后,当鼠标位于内部ListBox上,鼠标滚轮事件会被内部ListBox接收,导致外层ListBox不能用鼠标滚轮滑动!现在的需求是该事件要能给外部ListBox处理,即嵌套 ...

随机推荐

  1. python函数-匿名函数

    1,匿名函数,故名思意,就是没有名字的函数. def fun(x): return x+x 上述代码的作用就是求x+x的和的函数.如果调用该函数的话,可以使用fun(1)即可.那么lambda函数和上 ...

  2. .NET笔试题集(四)

    转载于:http://www.cnblogs.com/ForEvErNoME/archive/2012/09/10/2678727.html 1.请你简单的说明数据库建立索引的优缺点 使用索引可以加快 ...

  3. webservice安全性浅谈

    原文地址:http://www.cnblogs.com/chhuic/archive/2009/11/19/1606109.html 做项目时,经常会用到WebService来通讯,但WebServi ...

  4. css基础内容

    css基础内容 CSS 指层叠样式表 (Cascading Style Sheets)样式定义如何显示 HTML 元素样式通常存储在样式表中把样式添加到 HTML 4.0 中,是为了解决内容与表现分离 ...

  5. jQuery插件实例四:手风琴效果[无动画版]

    手风琴效果就是内容的折叠与打开,在这个插件中,使用了三种数据来源:1.直接写在DOM结构中:2.将数据写在配置项中:3.从Ajax()中获取数据.在这一版中,各项的切换没有添加动画效果,在下一版中会是 ...

  6. 有关java编辑器myeclipse编辑网站的一些设置(个人习惯)

    一.界面显示设置 首先进入一个新的空间,里面的设置肯定都是默认的.点击上方导航栏的window-Perferences-Appearance可以去进行设置界面的显示,Theme中可以选择windows ...

  7. File API

    ES5 推出了一系列的 API: BLOB (二进制大对象) File (文件接口,基于 BLOB,但是增加了文件相关的方法,比如路径,大小) FileList (借助 <input type= ...

  8. vlanif和vlan路由

    配置基于接口划分VLAN示例(接入层设备作为网关) 简介 划分VLAN的方式有:基于接口.基于MAC地址.基于IP子网.基于协议.基于策略(MAC地址.IP地址.接口).其中基于接口划分VLAN,是最 ...

  9. CSS中如何选择ul下li的奇数、偶数行

    <style> #Ulist li:nth-of-type(odd){ margin-left: 20px;}奇数行 #Ulist li:nth-of-type(even){margin- ...

  10. Nowcoder 提高组练习赛-R1

    https://www.nowcoder.com/acm/contest/172#question 单人报名300元,五人合报免费,于是就和学弟同学学长们组了一个三世同堂的队伍,高一的学长wzhqwq ...