WPF save listbox config
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
WPF save listbox config的更多相关文章
- WPF ItemsControl ListBox ListView比较
在进行列表信息展示时,WPF中提供多种列表可供选择.这篇博客将对WPF ItemsControl, ListBox, ListView进行比较. 相同点: 1. 这三个控件都是列表型控件,可以进行列表 ...
- WPF中ListBox的项ListBoxItem被选中的时候Background变化
使用WPF 中ListBox,点击ListBoxItem的时候,自定义它的背景色,曾经在网上找了一些方法, 不是很理想,后来在StackOverflow上找到了,贴出代码和效果图: 效果图:
- Android Kernel save defalut config
/********************************************************************************* * Android Kernel ...
- WPF中ListBox滚动时的缓动效果
原文:WPF中ListBox滚动时的缓动效果 上周工作中遇到的问题: 常规的ListBox在滚动时总是一格格的移动,感觉上很生硬. 所以想要实现类似Flash中的那种缓动的效果,使ListBox滚动时 ...
- [WPF系列]-ListBox
引言 本文就WPF中的ListBox常用项给以实例代码演示,包括隐蔽属性的设置,Style设置,以及ControlTemplate的自定义. Listbox平滑滚动 <ListBox Ite ...
- WPF中ListBox控件在选择模式(SelectionMode)为Single时仍然出现多个Item被选中的问题
最近在学习WPF过程中使用到了ListBox控件,在使用时遇到下面的奇怪问题: 代码如下: listBox.Items.Add("绘图"); listBox.Items.Add(& ...
- WPF的ListBox中的RadioButton不能单选问题
WPF不知道是微软故意弄的还是真的匆忙的推出的产品,在实际开发过程中有很多小问题解决很麻烦. 今天主要说一下ListBox中使用RadioButton的时候里面的RadioButton不能单选!居然成 ...
- WPF中ListBox /ListView如何改变选中条背景颜色
适用ListBox /ListView WPF中LISTVIEW如何改变选中条背景颜色 https://www.cnblogs.com/sjqq/p/7828119.html
- 【WPF】ListBox嵌套与事件冒泡
问题:两个ListBox嵌套后,当鼠标位于内部ListBox上,鼠标滚轮事件会被内部ListBox接收,导致外层ListBox不能用鼠标滚轮滑动!现在的需求是该事件要能给外部ListBox处理,即嵌套 ...
随机推荐
- css 给inline和inline-block元素设置margin和padding
经过简单的测试,行内元素或者行内块元素的maring-left,margin-right,padding-left,padding-right都是可以正常表现的,下面来看一下剩下的margin-top ...
- PostGIS安装
PostGIS安装 1.软件下载 postgresql-9.6.1-1-windows-x64-binaries.zip https://www.postgresql.org/download/win ...
- 带你从零学ReactNative开发跨平台App开发(二)
ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...
- [Android] 状态栏的一些认识
前段时间遇到几个关于状态栏的问题,又了解了一下状态栏相关的知识,现在做一下记录. 本文地址:http://www.cnblogs.com/rossoneri/p/4316343.html 前戏和问题 ...
- 参数innodb_force_recovery影响了整个InnoDB存储引擎的恢复状况
参数innodb_force_recovery影响了整个InnoDB存储引擎的恢复状况.该值默认为0,表示当需要恢复时执行所有的恢复操作.当不能进行有效恢复时,如数据页发生了corruption,My ...
- linux 通过 mac地址 查询 ip 和 清除arp 缓存
问题重述: 今天,突然找不到vm 的ip 了,但是可以从网卡状态上看到其 mac 地址,并且确定主机是启动状态,网络状态良好(后来发现因为子网掩码的问题,导致虚拟机和网关之间不通信,从而导致其他网络的 ...
- ODS设计
1.数据调研 2.确定数据范围 需要把上端应用需求与ODS数据范围进行验证,以确保应用所需的数据都已经从业务系统中抽取出来,并且得到了很好的组织,以ER模型表示数据主题关系 3.根据数据范围进行进一步 ...
- Spark 集群搭建
0. 说明 Spark 集群搭建 [集群规划] 服务器主机名 ip 节点配置 s101 192.168.23.101 Master s102 192.168.23.102 Worker s103 19 ...
- 解决windows下vim中文乱码
解决windows下vim中文乱码 windows安装了vim8,也就是gvim后,打开带有中文的文档,显示中文是乱码. 毕竟有许多文档我是用utf-8编码的,所以解决的办法是设置一下编码为utf-8 ...
- HTML5中的Canvas详解
什么是Canvas HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图形容器,您必须使用脚本来绘制图 ...