您的当前位置:首页正文

WPF使用心得

2021-07-22 来源:九壹网
WPF技巧

一、制作无边框窗体

XAML:

xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"

Width=\"620\" Height=\"370\" WindowStyle=\"None\" AllowsTransparency=\"True\" MouseLeftButtonDown=\"DragWindow\">

新建一个类: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 {

} } } }

因篇幅问题不能全部显示,请点此查看更多更全内容