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

颜色

设置背景色

1
2
System.Windows.Media.Color color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#4597FF");
btnConfirm.Background = new SolidColorBrush(color);

项目配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/// <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

1
2
3
4
5
6
7
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<!--0正式 1测试-->
<add key="IsDebug" value="1" />
</appSettings>
</configuration>

ListBox

注意

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

XAML

1
2
3
4
5
6
7
8
9
10
<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"/>

点击事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
}
}
}
}

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

设置选中索引即可

1
type_list_lb.SelectedIndex = -1;

多线程

线程切换

1
2
3
4
5
6
7
8
9
10
11
12
private async Task myFunc()
{
MyApp.Myloading.Show();
await Task.Run(
() =>
{
// 分线程耗时操作
Thread.Sleep(1000);
}
);
MyApp.Myloading.Hide();
}

延迟执行

1
2
3
4
5
6
7
8
9
10
11
internal class ZDelayUtil
{
public static void delay(int milliseconds, Dispatcher dispatcher, Action act)
{
Task.Run(() =>
{
Thread.Sleep(milliseconds);
dispatcher.Invoke(act);
});
}
}

调用

1
2
3
4
5
6
7
8
ZDelayUtil.delay(
3000,
Dispatcher,
() =>
{
tip_outer.Visibility = Visibility.Hidden;
}
);