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{
}
}
}