wpf+dev常用技巧

监视代码所花费的时间

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();
      }
  }
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值