前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF开发中常用的一些方法汇总

WPF开发中常用的一些方法汇总

作者头像
码客说
发布2022-06-04 17:23:23
2450
发布2022-06-04 17:23:23
举报
文章被收录于专栏:码客码客

颜色

设置背景色

代码语言:javascript
复制
System.Windows.Media.Color color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#4597FF");
btnConfirm.Background = new SolidColorBrush(color);

项目配置

代码语言:javascript
复制
/// <summary>
/// 从配置文件获取Value
/// </summary>
/// <param name="key">配置文件中key字符串</param>
/// <returns></returns>
public static string GetConfigValue(string key)
{
    try
    {
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //获取AppSettings的节点 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        return appsection.Settings[key].Value;
    }
    catch
    {
        return "";
    }
}

/// <summary>
/// 设置配置文件
/// </summary>
/// <param name="key">配置文件中key字符串</param>
/// <param name="value">配置文件中value字符串</param>
/// <returns></returns>
public static bool SetConfigValue(string key, string value)
{
    try
    {
        //打开配置文件 
        Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        //获取AppSettings的节点 
        AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
        appsection.Settings[key].Value = value;
        config.Save();

        return true;
    }
    catch
    {
        return false;
    }
}

配置文件App.config

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <!--0正式 1测试-->
    <add key="IsDebug" value="1" />
  </appSettings>
</configuration>

ListBox

注意

点击事件不要添加在Item的模板中,除非模板中有多个可点击的项。

XAML

代码语言:javascript
复制
<ListBox
         x:Name="type_list_lb"
         Grid.Column="1"
         Background="#f3f3f3"
         BorderThickness="0"
         ItemContainerStyle="{StaticResource ListBoxItemContainerStyle1}"
         ItemTemplate="{StaticResource typeItemDT}"
         ItemsSource="{Binding typeList}"
         Template="{StaticResource ListBoxTemplateH}"
         SelectionChanged="type_list_lb_SelectionChanged"/>

点击事件

代码语言:javascript
复制
private async void type_list_lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
  object sitem = type_list_lb.SelectedItem;
  if (sitem == null)
    return;

  if (sitem is ZListItemModel)
  {
    ZListItemModel item = (ZListItemModel)sitem;

    ObservableCollection<ZListItemModel> typeList = pageData.typeList;
    for (int i = 0; i < typeList.Count; i++)
    {
      ZListItemModel item_temp = typeList[i];
      if (item_temp == item)
      {
        item_temp.selected = 1;
      }
      else
      {
        item_temp.selected = 0;
      }
    }
  }
}

如果想让选中的项能够再次点击

设置选中索引即可

代码语言:javascript
复制
type_list_lb.SelectedIndex = -1;

多线程

线程切换

代码语言:javascript
复制
private async Task myFunc()
{
    MyApp.Myloading.Show();
    await Task.Run(
        () =>
        {
            // 分线程耗时操作
			Thread.Sleep(1000);
        }
    );
    MyApp.Myloading.Hide();
}

延迟执行

代码语言:javascript
复制
internal class ZDelayUtil
{
    public static void delay(int milliseconds, Dispatcher dispatcher, Action act)
    {
        Task.Run(() =>
                 {
                     Thread.Sleep(milliseconds);
                     dispatcher.Invoke(act);
                 });
    }
}

调用

代码语言:javascript
复制
ZDelayUtil.delay(
    3000, 
    Dispatcher, 
    () =>
    {
        tip_outer.Visibility = Visibility.Hidden;
    }
);
本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-01-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 颜色
    • 设置背景色
    • 项目配置
    • ListBox
    • 多线程
      • 线程切换
        • 延迟执行
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
        http://www.vxiaotou.com