DatePicker 控件日期格式化,可以在App.xaml.cs中添加下面代码
方法一 不推荐:
Thread.CurrentThread.CurrentCulture = (CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern = "yyyy-MM-dd";
方法二:
I have solved this problem with a help of this code. Hope it will help you all as well.
<Style TargetType="{x:Type DatePickerTextBox}"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, StringFormat='dd MMM yyyy', RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" /> </ControlTemplate> </Setter.Value> </Setter> </Style>
方法三:
Converter class:
public class DateFormat : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null) return null;
return ((DateTime)value).ToString("dd-MMM-yyyy");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
WPF tag
<DatePicker Grid.Column="3" SelectedDate="{Binding DateProperty, Converter={StaticResource DateFormat}}" Margin="5"/>
FileHelper.cs
#region 01.读取文件方法 —— static string ReadFile(string path)
/// <summary>
/// 读取文件方法
/// </summary>
/// <param name="path">路径</param>
/// <returns>内容字符串</returns>
/// <exception cref="ArgumentNullException">路径为空</exception>
public static string ReadFile(string path)
{
#region # 验证参数
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path), @"路径不可为空!");
}
#endregion
StreamReader reader = null;
try
{
reader = new StreamReader(path, Encoding.UTF8);
string content = reader.ReadToEnd();
return content;
}
finally
{
reader?.Dispose();
}
}
#endregion
#region 02.写入文件方法 —— static void WriteFile(string path, string content)
/// <summary>
/// 写入文件方法
/// </summary>
/// <param name="path">路径</param>
/// <param name="content">内容</param>
/// <param name="append">是否附加</param>
/// <exception cref="ArgumentNullException">路径为空</exception>
public static void WriteFile(string path, string content, bool append = false)
{
#region # 验证参数
if (string.IsNullOrWhiteSpace(path))
{
throw new ArgumentNullException(nameof(path), "路径不可为空!");
}
#endregion
FileInfo file = new FileInfo(path);
StreamWriter writer = null;
if (file.Exists && !append)
{
file.Delete();
}
try
{
//获取文件目录并判断是否存在
string directory = Path.GetDirectoryName(path);
if (string.IsNullOrEmpty(directory))
{
throw new ArgumentNullException(nameof(path), "目录不可为空!");
}
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
writer = append ? file.AppendText() : new StreamWriter(path, false, Encoding.UTF8);
writer.Write(content);
}
finally
{
writer?.Dispose();
}
}
#endregion
JsonHelper.cs
/// <summary>
/// JSON辅助操作类
/// </summary>
public static class JsonHelper
{
/// <summary>
/// 处理Json的时间格式为正常格式
/// </summary>
public static string JsonDateTimeFormat(string json)
{
json = Regex.Replace(json,
@"\/Date((d+))\/",
match =>
{
var dt = new DateTime(1970, 1, 1);
dt = dt.AddMilliseconds(long.Parse(match.Groups[1].Value));
dt = dt.ToLocalTime();
return dt.ToString("yyyy-MM-dd HH:mm:ss.fff");
});
return json;
}
/// <summary>
/// 把对象序列化成Json字符串格式
/// </summary>
/// <param name="object"></param>
/// <returns></returns>
public static string ToJson(object @object)
{
var json = JsonConvert.SerializeObject(@object);
return JsonDateTimeFormat(json);
}
/// <summary>
/// 把Json字符串转换为强类型对象
/// </summary>
public static T FromJson<T>(string json)
{
json = JsonDateTimeFormat(json);
return JsonConvert.DeserializeObject<T>(json);
}
}
WPF下载文件
WPF下载远程文件,并显示进度条和百分比
WPF下载远程文件,并显示进度条和百分比
1、xaml
< ProgressBar HorizontalAlignment="Left" Height="10" Margin="96,104,0,0" Name="pbDown" VerticalAlignment="Top" Width="100"/> < Label Content="Label" Name="label1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="206,104,0,0"/> |
2、CS程序
using System; using System.Windows; using System.Windows.Controls; using System.Net; using System.IO; using System.Threading; using System.Drawing; namespace WpfDemo { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); if (HttpFileExist( "http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg" )) { DownloadHttpFile( "http://183.62.138.31:57863/opt/resources/%E9%A3%8E%E6%99%AF/f1.jpg" , @"d:f1.jpg" ); } } public void DownloadHttpFile(String http_url, String save_url) { WebResponse response = null ; //获取远程文件 WebRequest request = WebRequest.Create(http_url); response = request.GetResponse(); if (response == null ) return ; //读远程文件的大小 pbDown.Maximum = response.ContentLength; //下载远程文件 ThreadPool.QueueUserWorkItem((obj) => { Stream netStream = response.GetResponseStream(); Stream fileStream = new FileStream(save_url, FileMode.Create); byte [] read = new byte [1024]; long progressBarValue = 0; int realReadLen = netStream.Read(read, 0, read.Length); while (realReadLen > 0) { fileStream.Write(read, 0, realReadLen); progressBarValue += realReadLen; pbDown.Dispatcher.BeginInvoke( new ProgressBarSetter(SetProgressBar), progressBarValue); realReadLen = netStream.Read(read, 0, read.Length); } netStream.Close(); fileStream.Close(); }, null ); } /// <summary> /// 判断远程文件是否存在 /// </summary> /// <param name="fileUrl">文件URL</param> /// <returns>存在-true,不存在-false</returns> private bool HttpFileExist( string http_file_url) { WebResponse response = null ; bool result = false ; //下载结果 try { response = WebRequest.Create(http_file_url).GetResponse(); result = response == null ? false : true ; } catch (Exception ex) { result = false ; } finally { if (response != null ) { response.Close(); } } return result; } public delegate void ProgressBarSetter( double value); public void SetProgressBar( double value) { //显示进度条 pbDown.Value = value; //显示百分比 label1.Content = (value / pbDown.Maximum) * 100 + "%" ; } } } |