WPF listview Test Message list
UI:

<Window x:Class="WoZhuLianyuanTool.SendContentsWind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d" Name="MWind"
Height="366" Width="617"
Title="MainWindow" ShowInTaskbar="False"
WindowStyle="None" Background="{x:Null}" AllowsTransparency="True"
MouseLeftButtonDown="Window_MouseLeftButtonDown" xmlns:my="clr-namespace:WoZhuLianyuanTool" WindowStartupLocation="CenterScreen" RenderTransformOrigin="0.5,0.5">
<Window.Resources> <my:StringToBoolConverter x:Key="StringToBoolConverter1" />
</Window.Resources>
<Window.RenderTransform>
<ScaleTransform x:Name="t" ScaleX="1" ScaleY="1"> </ScaleTransform>
</Window.RenderTransform>
<Window.Triggers>
<EventTrigger RoutedEvent="Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation From="0.5" To="1" Storyboard.TargetName="t" Storyboard.TargetProperty="ScaleX" Duration="0:0:2"> <DoubleAnimation.EasingFunction>
<ElasticEase Oscillations="3"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
<DoubleAnimation From="0.5" To="1" Storyboard.TargetName="t" Storyboard.TargetProperty="ScaleY" Duration="0:0:2"> <DoubleAnimation.EasingFunction>
<ElasticEase Oscillations="3"/>
</DoubleAnimation.EasingFunction>
</DoubleAnimation> </Storyboard> </BeginStoryboard>
</EventTrigger>
</Window.Triggers> <Grid RenderTransformOrigin="0.5,0.5"> <Grid.ColumnDefinitions>
<ColumnDefinition Width="561*" />
<ColumnDefinition Width="8*" />
</Grid.ColumnDefinitions> <Border CornerRadius="10,10,10,10" Margin="10,10,2,10" Height="Auto" BorderBrush="White" BorderThickness="6" Background="#FFEBEBEB">
<Border.Effect>
<DropShadowEffect Opacity="1" ShadowDepth="4">
</DropShadowEffect>
</Border.Effect>
<Grid Name="gridParent"> <Grid Name="gridTitle" Margin="0,0,0,259">
<Rectangle Fill="#FFFF7AA4" Height="42" VerticalAlignment="Top" Margin="0,0,0,0" /> <Label Name="lbVersion" Foreground="WhiteSmoke" Width="362" Height="24" Content="--------" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="8,5,0,0" Background="#02E2D3D3"/> </Grid> <Grid Margin="0,42,0,0">
<ListView Name="listView" ItemsSource="{Binding}" Height="179" Margin="0,42,0,32"> <ListView.View>
<GridView>
<GridViewColumn Width="26" Header="勾选" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Tag="{Binding id}" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" IsChecked="{Binding Path=isChecked2,Mode=OneTime}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn> <GridViewColumn Width="458" Header="msg" DisplayMemberBinding="{Binding msg}"/>
<GridViewColumn Width="66" Header="del" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<Button Tag="{Binding id}" Content="删除" Click="btnDel_Click"></Button>
</DataTemplate>
</GridViewColumn.CellTemplate> </GridViewColumn> </GridView>
</ListView.View>
</ListView> <TextBox Height="23" Margin="8,10,107,0" Name="txtMsg" VerticalAlignment="Top" />
<Button VerticalAlignment="Top" HorizontalAlignment="Right" Height="30" Width="86" Margin="0,6,5,0" Content="add" Name="btnAdd" Click="btnAdd_Click"></Button>
<Label VerticalAlignment="Bottom" Name ="lbStatus" Content="000"></Label> </Grid> </Grid>
</Border> <!--左上角的“X”叉形按钮-->
<Button x:Name="x" Content="Button" HorizontalAlignment="Right" Height="24" Style="{DynamicResource XButtonStyle}" VerticalAlignment="Top" Width="46" Margin="0,18,9,0" Click="btnClose_Click" /> </Grid>
</Window>
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.Globalization; namespace WoZhuLianyuanTool
{
/// <summary>
/// Interaction logic for SendContentsWind.xaml
/// </summary>
public partial class SendContentsWind : Window
{
public SendContentsWind()
{
InitializeComponent();
dbtool = new DBTools();
listData = dbtool.LoadSendContentItems();
listView.DataContext = listData;
listView.ItemsSource = listData;
lbStatus.Content = "共" + listData.Count + "项,已选" + listData.Count(n => n.isChecked2 == true);
Loaded += new RoutedEventHandler(SendContentsWind_Loaded); }
List<SendContentItem> listData = null;
DBTools dbtool = null;
void SendContentsWind_Loaded(object sender, RoutedEventArgs e)
{ } private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
} private void btnClose_Click(object sender, RoutedEventArgs e)
{
Close();
} private void btnAdd_Click(object sender, RoutedEventArgs e)
{ if (txtMsg.Text.Trim() == "") return;
dbtool.AddSendContentItem(new SendContentItem (){ id=Guid.NewGuid().ToString(), isChecked="True", msg=txtMsg.Text });
txtMsg.Text = "";
updateList();
listView.ScrollIntoView(listView.Items[listView.Items.Count -1]); }
private void btnDel_Click(object sender, RoutedEventArgs e)
{
dbtool.DelSendContentItem("" + (sender as Button ).Tag);
updateList();
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
// MessageBox.Show(""+(sender as CheckBox).IsChecked);
dbtool.UpdateSendContentItem("" + (sender as CheckBox).Tag,(bool )(sender as CheckBox).IsChecked);
updateList2(); } private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
CheckBox ck=(sender as CheckBox);
//MessageBox.Show("" + (sender as CheckBox).IsChecked);
dbtool.UpdateSendContentItem(""+ck.Tag,(bool)ck.IsChecked);
updateList2();
} private void updateList() { listData = dbtool.LoadSendContentItems();
listView.ItemsSource = listData;
lbStatus.Content = "共" + listData.Count + "项,已选" + listData.Count(n=>n.isChecked2==true );
} private void updateList2()//not rebind
{
listData = dbtool.LoadSendContentItems();
lbStatus.Content = "共" + listData.Count + "项,已选" + listData.Count(n=>n.isChecked2 ==true);
} } public class StringToBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value==null)
{
return true;
}
// int val = System.Convert.ToInt32(value);
bool b=true ;
if (value.ToString() == "0" || value.ToString().ToLower() == "false") { b = false;
} if (value.ToString() == "1" || value.ToString().ToLower() == "true")
{ b = false;
} return b;
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value==null)
{
return "0";
} return ((bool)value) ? "true" : "false";
}
} }
DBTool class:
//---------------------------------------------------------------------------
public bool CreateTable_SendContent()
{
bool isOK = false;
string strCmd = "";
SQLiteConnection conn = new SQLiteConnection(strConn);
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
SQLiteTransaction myTrans;
strCmd = @"select count(1) cnt from sqlite_master where type='table' and name = 'SendContent' ";
cmd.CommandText = strCmd;
int cnt = int.Parse("" + cmd.ExecuteScalar());
if (cnt <= 0)
{//add table
// Start a local transaction
myTrans = conn.BeginTransaction(System.Data.IsolationLevel.Serializable);
cmd.Transaction = myTrans;
strCmd = @"ALTER TABLE members ADD flag_no_ReSend integer DEFAULT 0";
strCmd = @"create table SendContent(id text,msg text,isChecked text,primary key (id))";
cmd.CommandText = strCmd;
cmd.ExecuteNonQuery();
cmd.Transaction.Commit();
FillTableSendContent();// fill test content
}
isOK = true;
}
catch (Exception ex)
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
isOK = false;
MessageBox.Show("SendContent:" + ex.Message);
}
return isOK;
}
/// <summary>
/// fill test contents
/// </summary>
/// <returns></returns>
public bool FillTableSendContent()
{
bool isOK = false;
string strCmd = "";
SQLiteConnection conn = new SQLiteConnection(strConn);
try
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
SQLiteTransaction myTrans;
// Start a local transaction
myTrans = conn.BeginTransaction(System.Data.IsolationLevel.Serializable);
cmd.Transaction = myTrans;
strCmd = @"insert into SendContent(id,msg,isChecked) select '"+ Guid.NewGuid().ToString()+"','test msg','1'";
cmd.CommandText = strCmd;
cmd.CommandText = strCmd;
cmd.ExecuteNonQuery();
cmd.Transaction.Commit();
isOK = true;
}
catch (Exception ex)
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
isOK = false;
MessageBox.Show("SendContent:" + ex.Message);
}
return isOK;
}
public List<SendContentItem> LoadSendContentItems() {
List<SendContentItem> list = new List<SendContentItem>();
DataTable tb = getTable("select * from SendContent");
if(tb!=null && tb.Rows.Count>0){
foreach (DataRow r in tb.Rows) {
list.Add(new SendContentItem() { id = "" + r["id"], msg = "" + r["msg"], isChecked = "" + r["isChecked"] });
}
}
return list;
}
public bool UpdateSendContentItem(string id, bool isChecked, string newMsg="")
{
int i = 0;
if (newMsg == "")
{
i = ExeSql("update SendContent set isChecked='" + isChecked + "' where id='" + id + "'");
}
else {
i = ExeSql("update SendContent set msg='"+ newMsg +"', isChecked='" + isChecked + "' where id='" + id + "'");
}
return i> 0;
}
public bool AddSendContentItem(SendContentItem item){
int i = 0;
i = ExeSql( @"insert into SendContent(id,msg,isChecked) select '"+ Guid.NewGuid().ToString()+"','"+item.msg +"','"+ item.isChecked+"'");
return i > 0;
}
public bool DelSendContentItem(string id )
{
int i = 0;
i = ExeSql( @"delete from SendContent where id='"+ id +"'");
return i > 0;
}
//---------------------------------------------------------------------------
public class SendContentItem {
public string id { get; set; }
public string msg { get; set; }
public string isChecked { get; set; }
public bool isChecked2 {
get{
return isChecked=="1" || isChecked.ToLower()=="true";
}
set{
}
}
}
WPF listview Test Message list的更多相关文章
- WPF ListView 选中问题
WPF ListView 选中问题 摘自:http://www.cnblogs.com/BBHor/archive/2013/04/28/VisualTreeHelper-PreviewMouseD ...
- [WPF]ListView点击列头排序功能实现
[转] [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...
- WPF ListView 居中显示
原文:WPF ListView 居中显示 今天遇到的问题: 方法1:设置GridViewColumn的ActualWidth <ListView > <ListView.View&g ...
- WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...
- WPF listview item mouse enter/over popup
This is because the routing strategy of the Loaded event is Direct, which means that the routed even ...
- wpf ListView DataTemplate方式的鼠标悬停和选中更改背景色
今天使用wpf技术弄一个ListView的时候,由于需求需要,需要ListView显示不同的数据模板,很自然的使用了DataTemplate方式来定义多个数据模板,并在ListView中使用ItemT ...
- WPF ListView点击删除某一行并获取绑定数据
最近在开发WPF程序时遇到一个问题,在gridview中希望实现在每一行最后添加一个删除的按钮,但是发现点击每行的button时只会触发button的点击事件,并没有选中这一行,此时调用list.Se ...
- wpf listview
<Window x:Class="WpfTutorialSamples.ListView_control.ListViewGridViewSample" xml ...
- wpf listview 换行
<ListView Name="listView1" VerticalAlignment="Top" Height="600" Ma ...
随机推荐
- jsp登录显示
1.登录成功设置session request.getSession().setAttribute("user", user); 2.前台test <div class=&q ...
- npm、cnpm、bower安装区别
简单地说,就是帮你下载好你需要的css或者js库,而且三者功能也都是一样的.那为什么要下载这3个不同的呢?据说npm容易被墙……而cnpm是淘宝的镜像,所以通常用cnpm代替npm.至于bower,是 ...
- web测试实践
参会人员:赵天宇,周静,张双双,张玉 参会地点:微信群 参会内容:决定评测软件 最后会议结论:决定了选择用中国大学mooc(https://www.icourse163.org/)和结合竞品对象-清华 ...
- c#WebApi使用form表单提交excel,实现批量写入数据库
思路:用户点击下载模板按钮,获取到excel模板,然后向里面填写数据保存.from表单提交的时候选择保存好的excel,实现数据的批量导入过程 先把模板放在服务器的项目目录下面:如 模板我一般放在:F ...
- SQLSERVER文件组误脱机后如何联机
场景:在学习文件组的恢复过程中,通过 ALTER DATABASE TEST MODIFY FILE(NAME = SUBF,OFFLINE) 把文件组给弄脱机了.这时却发现脱机之前忘记备份了. 这时 ...
- C# 倒计时
c#中有一个叫做timespan的数据类型,可以这样构造: TimeSpan ts = , , ); TimeSpan(hour,minute,second); 然后拖进去一个timer,叫timer ...
- PostgreSQL数据加载工具之pg_bulkload
1. 介绍 PostgreSQL提供了一个copy命令的便利数据加载工具,copy命令源于PostgreSQL数据库,copy命令支持文件与表之间的数据加载和表对文件的数据卸载.pg_bulkload ...
- CameraAPI中的 自定义照相功能
前几天的项目需要使用CameraAPI自己定义照相机,之前用过的二维码也要自己写底层代码,于是总结一下使用CameraAPI的几点事项.现在由于JDK7.0及其以上版本的官方文档已经不再推荐使用cam ...
- ShellCode初体验
写在前面的话: ShellCode是一门艺术,就像围棋手门追求的“神之一手”,今天就来初探一下这让人疯狂的艺术: 零.代码0 相信手写opcode,目前很少有人干了,其实,也确实已经没有这个必要了,毕 ...
- 利用MVC Chart 打造后台图表、前端图表
应用场景是这个样子的:要做导出数据到PDF的功能,涉及到文本.表格.图表等内容.在做图表功能时,发现之前用Highcharts做的图表根本就不能集成到PDF中.这里需要一个能在程序后台就生成图表的功能 ...