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处理,即嵌套 ...
随机推荐
- 使用excel开发平台活字格搭建物流管理系统
物流管理系统是指包含完整的物流公司信息.物流寄件信息.物流运输信息以及物流寄件单位等管理系统.功能完善的物流管理系统,能帮助物流企业更好的进行物流管理. 下面我们借助活字格,来设计一个简单的物流管理系 ...
- Visual Studio Code必备插件
HTML Snippets: 超级实用且初级的 H5代码片段以及提示 HTMLHint: html代码检测 HTML CSS Support : 让 html 标签上写class 智能提示当前项目所支 ...
- java基础(十一) 枚举类型
枚举类型Enum的简介 1.什么是枚举类型 枚举类型: 就是由一组具有名的值的有限集合组成新的类型.(即新的类). 好像还是不懂,别急,咱们先来看一下 为什么要引入枚举类型 在没有引入枚举类型前,当我 ...
- 记一款bug管理系统(bugdone.cn)的开发过程(4) - 新增BugTalk功能
测试人员提出一个Bug,如果开发人员对Bug有疑义,会直接面对面讨论或者通过QQ等线上聊天工具讨论,但过后再去找讨论记录会很麻烦.因此BugDone提出一个全新的概念:将问题的讨论留在问题内.BugD ...
- win10 安装microsoft.net framework3.5
转载于:https://www.windows10.pro/win10-net-framework-3-5/ 之前手残不小心把microsoft.net framework3.5删了,结果导致Sql ...
- 适用于 Windows 的自定义脚本扩展
自定义脚本扩展在 Azure 虚拟机上下载并执行脚本. 此扩展适用于部署后配置.软件安装或其他任何配置/管理任务. 可以从 Azure 存储或 GitHub 下载脚本,或者在扩展运行时将脚本提供给 A ...
- CSS| 實例---寬度自由調節button,圖片切換
<html lang="en"> <head> <meta charset="utf-8"/> <title>I ...
- 汇编语言debug命令与指令机器码
一.debug命令 二.标志信息 -r用这个指令,得到的信息右下角: NV UP EI PL NZ NA PO NC 这些符号代表的就是标志寄存器里常用标志位的值.这个是符号值对应表: 溢出标志OF( ...
- 数据库迁移之从oracle 到 MySQL最简单的方法
数据库迁移之从oracle 到 MySQL最简单的方法 因工作需要将oracle数据库换到MySQL数据库,数据量比较大,百万级别的数据,表也比较多,有没有一种既快捷又安全的方法呢?答案是肯定的,下面 ...
- vsftp配置
网上很多,但我还是想再整理一份属于自己的 1.vsftp简介 vsftp提供三种登陆方式:.匿名登录 .本地用户登录 .虚拟用户登录 vsftpd的特点:.较高的安全性需求 .带宽的限制 .创建支持虚 ...