记录WPF的技巧(二)21-40

目录

21.强制退出、加载控件没有值报错、加载控件显示类名

22.查询datagrid中,模板中,TextBox中的Background颜色

23.WPF中全局捕捉异常

24.WPF中RichTextBox一秒弹一个展示数据

25.HandyControl控件

26.WPF事件性能问题处理

27.快速创建WPF,使用Template Studio for WPF

28.wpf使用默认数据源

29.wpf中使用XPath

30.wpf快速关闭程序的方法


21.强制退出、加载控件没有值报错、加载控件显示类名

WPF强制退出程序

            Environment.Exit(0);

控件加载数据源的时候没有值,运行的时候报错

public event PropertyChangedEventHandler PropertyChanged = delegate { };

控件加载显示类名,不显示具体数据的时候 

效果

解决方法2种

1.在实体类中重载一下 

 public override string ToString()
        {
            return Name;
        }

2.在控件的数据模板中修改

        <ComboBox Name="a" Width="100" Height="20" >
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Name}"/>
                        <TextBlock Text="{Binding Class}"/>
                    </StackPanel>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

最终效果

22.查询datagrid中,模板中,TextBox中的Background颜色

private void GetTextBoxBackground(Visual myVisual)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
            {
                Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);
                if (childVisual is TextBox)
                {
                    if ((childVisual as TextBox).Background != null && (childVisual as TextBox).Background.ToString() == "#FFFF0000")
                    {
                        isBackground = true;
                        return;
                    }
                }
                else
                {
                    GetTextBoxBackground(childVisual);
                }
            }
        }

GetTextBoxBackground(dgPerson);调用

23.WPF中全局捕捉异常

1.App.xaml

<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml"
             DispatcherUnhandledException="App_OnDispatcherUnhandledException">
    <Application.Resources>

    </Application.Resources>
</Application>

2.App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace WpfApp1
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void App_OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("程序出现严重错误" + e.Exception.Message);
            e.Handled = true;
        }
    }
}

24.WPF中RichTextBox一秒弹一个展示数据

xaml

<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <StackPanel>
            <Button Name="btnThd" Click="btnThd_Click" >一秒弹出一个</Button>
            <RichTextBox x:Name="a" Height="100">
                <FlowDocument>
                    <Paragraph>
                        <Run Text="RichTextBox"/>
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
        </StackPanel>
    </Grid>
</Window>

cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
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.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace WpfApp3
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private async void btnThd_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 100; i++)
            {
                await Task.Delay(1000);
                await Task.Run(ModifyA);
            }

        }
        private void ModifyA()
        {
            //模拟一些工作耗时的工作
            this.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
            {
                a.AppendText("1");
            }));
        }
    }
}

效果

25.HandyControl控件

HandyControl是wpf中著名的控件

中文文档是欢迎使用HandyControl | HandyOrg

菜单栏的使用

<hc:Window x:Class="HCDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:HCDemo"
        xmlns:hc="https://handyorg.github.io/handycontrol"
        mc:Ignorable="d"
        Background="{DynamicResource RegionBrush}"
        Title="MainWindow" Height="450" Width="800" Icon="/imgs/logo.ico">

    <hc:Window.NonClientAreaContent >
        <StackPanel   Orientation="Horizontal">
            <hc:Shield Subject="c#" Status="7.0" HorizontalAlignment="Left" Margin="4,0,0,0" Color="#1182c3"/>
            <Button Margin="5,0,0,0" Content="菜单1"/>
            <TextBlock Text="菜单2" VerticalAlignment="Center" Margin="5,0,0,0" />
        </StackPanel>
    </hc:Window.NonClientAreaContent>
    <Grid>
        <TextBlock Text="123" />
    </Grid>
</hc:Window>

效果 

26.WPF事件性能问题处理

  e.Handled = true;

避免向上传递,降低性能。 

27.快速创建WPF,使用Template Studio for WPF

28.wpf使用默认数据源

    <StackPanel>
        <DataGrid d:ItemsSource="{d:SampleData ItemCount=5}"/>
        <ListBox d:ItemsSource="{d:SampleData ItemCount=6}"/>
    </StackPanel>

29.wpf中使用XPath

wpf中使用XPath或者XMLDataProvider可以对xml文件进行绑定操作

30.wpf快速关闭程序的方法

Process.GetCurrentProcess().Kill();

来源:记录WPF的技巧(二)21-40_wpf baseon-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故里2130

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值