一、制作无边框窗体
XAML:
Width=\"620\" Height=\"370\" WindowStyle=\"None\" AllowsTransparency=\"True\" MouseLeftButtonDown=\"DragWindow\">
CS:
using System;
using System.Collections.Generic; 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;
namespace Default.Window1 {
/// /// NetCrucialKpiDetail.xaml 的交互逻辑 ///
public partial class Window1: Window {
public Window1() {
InitializeComponent(); }
public void DragWindow(object sender, MouseButtonEventArgs args) {
this.DragMove(); }
public void CloseWindow(object sender, RoutedEventArgs args) {
this.Close(); } } }
二、ListView交替背景色
XAML:
新建一个类:BackgroundConverter.cs
using System;
using System.Collections.Generic; using System.Linq; using System.Text;
using System.Windows.Data; using System.Windows.Controls; using System.Windows.Media; using System.Globalization;
public sealed class BackgroundConverter : IValueConverter {
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
ListViewItem item = (ListViewItem)value;
ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
// Get the index of a ListViewItem
int index = listView.ItemContainerGenerator.IndexFromContainer(item); if (index % 2 == 0) {
return Brushes.Moccasin; } else {
return Brushes.White; } }
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
return null; } }
三、WPF中listview的全选、全不选
全选:listview.SelectAll();
全不选:listview.SelectedItems.Clear();
四、一个写日志的类
using System;
using System.Collections.Generic; using System.Linq; using System.Text; using System.IO;
public class Log {
//存放日志文件的文件夹名称
private const string tempFilename = \"ServiceLog\";
//filename 日志文件名 text要写入日志的文本 public static void WriteLine(string filename, string text) {
lock (typeof(Log)) {
try {
//存放日志文件夹的全路径
string file = String.Format(@\"C:\\Temp\\{0}\\\
Directory.CreateDirectory(file);
file = String.Format(\"{0}{1}.log\
if (File.Exists(file) && (new FileInfo(file)).Length > 5 * 1024 * 1024) {
string newFile = file.Remove(file.Length - 4, 4) + \"_bak.log\"; File.Copy(file, newFile, true); File.Delete(file); }
using (StreamWriter sw = new StreamWriter(file, true, Encoding.Unicode)) {
sw.WriteLine(String.Format(\"[{0}] \DateTime.Now.ToString(\"yyyy-MM-dd HH:mm:ss\")) + text); sw.Flush(); } } catch {
} } } }
因篇幅问题不能全部显示,请点此查看更多更全内容