wpf+dev常用技巧
- 监视代码所花费的时间
- WPF获取所有窗体、获取当前主窗体、窗体居中
- 耗时久的操作,增加转圈界面(SplashScreenManager)
- xaml样式设计:dgx编写Expression规则,实现单元格特定条件标红
- 异步:消息弹窗的异步
- xaml样式设计:多层级,获取元素,设置它的属性
- xaml样式设计:当没选择日期时,默认的值颜色为红色
- xaml样式设计:添加阴影效果(DropShadowEffect)
- devExpress表格编写规则,当价格大于另一价格时,颜色标红
- System.Windows.TextTrimming 文本修饰,文本最后加...省略符
- TreeListView.CustomColumnDisplayText重绘表格界面的内容
监视代码所花费的时间
protected void StopwatchTest()
{
System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start(); // 开始监视代码
//_________________要执行的函数______________________
//Code……
stopwatch.Stop(); // 停止监视
TimeSpan timeSpan = stopwatch.Elapsed; // 获取总时间
double hours = timeSpan.TotalHours; // 小时
double minutes = timeSpan.TotalMinutes; // 分钟
double seconds = timeSpan.TotalSeconds; // 秒数
double milliseconds = timeSpan.TotalMilliseconds; // 毫秒数
}
WPF获取所有窗体、获取当前主窗体、窗体居中
//1、获取所有窗体
var getAllWindows = Application.Current.Windows;
//2、获取当前主窗体
var getAllWindows = Window.GetWindow(Application.Current.MainWindow);
//3、窗体居中
WindowStartupLocation.CenterOwner;
耗时久的操作,增加转圈界面(SplashScreenManager)
using System.Windows;
SplashScreenManager splashManager = SplashScreenManager.CreateWaitIndicator(new DevExpress.Mvvm.DXSplashScreenViewModel
{
Status = "正在导入数据,请稍候...",
});
splashManager.Show(Application.Current.MainWindow, WindowStartupLocation.CenterOwner);
try
{
/*执行导入操作*/
splashManager.Close(); //执行完毕之后,关闭转圈。
}
catch (Exception ex)
{
splashManager.Close(); //有异常时,关闭界面。
}
splashManager.Close();
SplashScreenManager 两种Show的方式介绍
//自己定义展示的位置,展示的方式
public void Show(DependencyObject owner = null, WindowStartupLocation startupLocation = WindowStartupLocation.CenterOwner, bool trackOwnerPosition = true, InputBlockMode inputBlock = InputBlockMode.None, int timeout = 700);
//自动关闭
public void ShowOnStartup(bool autoClose = true);
var windows = Application.Current.MainWindow //获取所有窗体
var owner = Application.Current.MainWindow //获取主窗体
var position1 = WindowStartupLocation.CenterOwner //owner的中间
var position2 = WindowStartupLocation.CenterScreen //屏幕的中间
var position2 = WindowStartupLocation.Manual //随机位置
//展示在父级窗体的中间
splashManager.Show(Application.Current.MainWindow, WindowStartupLocation.CenterOwner);
xaml样式设计:dgx编写Expression规则,实现单元格特定条件标红
1、dxg的数据源内容
2、代码
3、最终效果
异步:消息弹窗的异步
#region 异步
public static void ShowMessageAsync(string message, string buttonContent = "我知道了")
{
App.Current.Dispatcher.Invoke((Action)(() =>
{
TrueLoreMessageBox.ShowMessage(message, buttonContent);
}));
}
public static bool ShowMessageDoubleButtonAsync(string message, string okContent = "确定", string cancelContent = "我再想想")
{
return App.Current.Dispatcher.Invoke(new Func<bool>(() =>
{
return TrueLoreMessageBox.ShowMessageDoubleButton(message, okContent, cancelContent);
}));
}
public static void ShowWarningAsync(string message, string buttonContent = "我知道了")
{
App.Current.Dispatcher.Invoke((Action)(() =>
{
TrueLoreMessageBox.ShowWarning(message, buttonContent);
}));
}
/// <summary>
/// type=0:是否; type=1:我再想想
/// </summary>
public static bool? ShowBooleanConfirmAsync(string message, int type = 0)
{
return App.Current.Dispatcher.Invoke(new Func<bool?>(() =>
{
return TrueLoreMessageBox.ShowBooleanConfirm(message, type);
}));
}
#endregion
xaml样式设计:多层级,获取元素,设置它的属性
需求:xaml当鼠标移动到该按钮时,需要更改投影的透明度。
xaml样式设计:当没选择日期时,默认的值颜色为红色
注意在XAML中,值为NULL,需要像以下这样写,不能直接写Null.
Value="{x:Null}"
xaml样式设计:添加阴影效果(DropShadowEffect)
转载:https://www.pianshen.com/article/42941875128/
devExpress表格编写规则,当价格大于另一价格时,颜色标红
效果:
System.Windows.TextTrimming 文本修饰,文本最后加…省略符
当文本超出控件长度时,使用TextTrimming 可以使超长的文本变成…省略符。
public enum TextTrimming
{
//无修饰
None = 0,
//单字符修饰,这个用得多
CharacterEllipsis = 1,
//单词修饰
WordEllipsis = 2
}
通常搭配ToolTip进行使用。ToolTip当鼠标移动到这个地方的时候,会进行提示。
用法:
<TextBlock x:Name="txt" Text="{TemplateBinding Header}"
ToolTip="{TemplateBinding Header}"
TextTrimming="CharacterEllipsis"/>
TreeListView.CustomColumnDisplayText重绘表格界面的内容
e.Node.Content 其实就是一个DataRow
//*********** xaml界面***********
<dxg:TreeListControl.View>
<dxg:TreeListView
x:Name="treeListView商务标模拟得分汇总"
CustomColumnDisplayText="treeListView_CustomColumnDisplayText"/>
</dxg:TreeListControl.View>
//*********** 代码***********
private void treeListView商务标模拟得分汇总_CustomColumnDisplayText(object sender, DevExpress.Xpf.Grid.TreeList.TreeListCustomColumnDisplayTextEventArgs e)
{
if (e.Node == null)
return;
if (e.Column.FieldName == "XH")
{
e.DisplayText = (this.treeListView.Nodes.IndexOf(e.Node) + 1).ToString();
}
}