一、前言
在 WPF 开发中,资源(Resource) 与 数据绑定(Data Binding) 是两个最基础也最常用的机制。资源用于在 XAML 中集中定义可复用的对象(字符串、数组、颜色、画刷等);数据绑定则负责把界面元素与后台数据对象关联起来,实现“数据变化 → 界面自动更新”。
本文基于 .NET Framework 4.8 + Visual Studio 2022,用一个完整示例演示:
- 如何在
<Window.Resources>中定义字符串、数组、颜色、画刷等资源; - 如何通过
{StaticResource key}使用资源; - 如何通过
DataContext+{Binding 属性名}完成数据绑定。
二、资源绑定
1. 在 Window.Resources 中定义资源
资源可以是简单的整型、字符串、数组、颜色等类型的数据。定义资源前,需要先引入对应的命名空间:```xml
xmlns:sys=“clr-namespace:System;assembly=mscorlib”
然后在 `<Window.Resources>` 中定义:
```xml
<Window.Resources>
<!-- 定义一个字符串,初始值为“成功” -->
<sys:String x:Key="str1">成功</sys:String>
<!-- 定义一个字符串数组 -->
<x:Array x:Key="array1" Type="sys:String">
<sys:String>坚果</sys:String>
<sys:String>蘑菇</sys:String>
<sys:String>木瓜</sys:String>
</x:Array>
<!-- 定义一个整型数组 -->
<x:Array x:Key="array2" Type="sys:Int32">
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
</x:Array>
<!-- 定义一个画刷资源 -->
<SolidColorBrush x:Key="s1" Color="Red"/>
<!-- 定义一个颜色数组 -->
<x:Array x:Key="s2" Type="Color">
<Color>Red</Color>
<Color>Green</Color>
<Color>Blue</Color>
</x:Array>
</Window.Resources>
2. 使用资源
使用资源时,通过 {StaticResource key 名} 引用即可:
<StackPanel>
<!-- 引用字符串资源 -->
<TextBlock Text="{StaticResource str1}"/>
<!-- 引用字符串数组作为 ListBox 的数据源 -->
<ListBox Width="100" Height="100" ItemsSource="{StaticResource array1}"/>
<!-- 引用整型数组作为数据源 -->
<ListBox Width="100" Height="100" ItemsSource="{StaticResource array2}"/>
<!-- 同时引用数组资源与画刷资源 -->
<ListBox Width="100" Height="100"
ItemsSource="{StaticResource array2}"
Background="{StaticResource s1}"/>
<!-- 引用颜色数组作为数据源 -->
<ListBox Width="100" Height="100"
ItemsSource="{StaticResource s2}"
Background="{StaticResource s1}"/>
</StackPanel>
说明:
StaticResource在 XAML 加载时一次性查找并赋值,适合资源在加载后不再变化的场景;若资源会动态替换,应使用DynamicResource。
三、数据绑定
数据绑定的核心是 DataContext(数据上下文)与 {Binding 属性名}。完整步骤如下。
1. 定义实体类
namespace _3绑定值.Model
{
public class People
{
public string Name { get; set; } = "张三";
public int Age { get; set; } = 18;
public List<string> list { get; set; } = new List<string> { "篮球", "足球", "羽毛球" };
}
}
2. 引入实体类所在命名空间
xmlns:vm="clr-namespace:_3绑定值.Model"
3. 通过 Window.DataContext 指定数据上下文
<Window.DataContext>
<!-- 当前窗体所使用的上下文对象 -->
<vm:People></vm:People>
</Window.DataContext>
4. 将资源写入资源字典(ResourceDictionary)
当资源需要在多个窗口、多个控件之间共享时,把它们全部堆在 <Window.Resources> 里会导致重复定义、难以维护。此时可以把资源抽取到独立的资源字典(ResourceDictionary)中,再在需要的地方合并引用。
(1)新建资源字典文件
在项目中右键 → 添加 → 新建项 → 选择「资源字典(WPF)」,命名为 Styles.xaml,其根节点就是 <ResourceDictionary>:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<!-- 字符串资源 -->
<sys:String x:Key="str1">成功</sys:String>
<!-- 字符串数组 -->
<x:Array x:Key="array1" Type="sys:String">
<sys:String>坚果</sys:String>
<sys:String>蘑菇</sys:String>
<sys:String>木瓜</sys:String>
</x:Array>
<!-- 画刷资源 -->
<SolidColorBrush x:Key="s1" Color="Red"/>
</ResourceDictionary>
(2)在窗口(或 App.xaml)中合并资源字典
通过 MergedDictionaries 把资源字典合并进来,之后就能像使用本地资源一样用 {StaticResource key名} 引用:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- Source 使用相对路径或 pack URI -->
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
若希望整个应用的所有窗口都能用,则把上面的合并写在 App.xaml 的 <Application.Resources> 中:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
(3)资源字典的优势
- 集中管理、便于复用:字符串、画刷、样式统一定义在一处,多个窗口/控件共享同一份资源,避免重复代码。
- 便于维护与统一风格:修改资源只需改字典文件一处,所有引用处自动生效,利于统一 UI 风格。
- 支持模块化拆分:可按功能拆成多个字典(如
Colors.xaml、Styles.xaml、Templates.xaml),通过MergedDictionaries组合,结构清晰。 - 便于换肤与主题切换:把不同主题写成不同字典,运行时替换
MergedDictionaries中的字典即可实现换肤(配合DynamicResource效果更佳)。 - 可跨程序集共享:资源字典可通过 pack URI 被其他程序集引用,适合做公共 UI 库。
提示:
Source路径要写对,若字典放在子目录中应写成Source="Resources/Styles.xaml";跨程序集引用则使用pack://application:,,,/程序集名;component/路径/Styles.xaml形式。
5. 通过 {Binding 属性名} 进行绑定
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}"/>
<ListBox Width="100" Height="100"
ItemsSource="{Binding list}"
Background="{StaticResource s1}"/>
</StackPanel>
6. 传统方式对比:在 Load 事件中赋值
在 WinForm 中,通常是在窗体的 Load 事件里给控件的数据源赋值。WPF 中同样可以这样做,但更推荐使用绑定:```csharp
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// 传统方式:手动赋值
listbox.ItemsSource = new List { “坚果”, “蘑菇”, “木瓜” };
}
```xml
<ListBox Name="listbox" Width="100" Height="100"
SelectionChanged="listbox_SelectionChanged"/>
对比可见,绑定方式无需在后台代码中手动赋值,界面与数据即可自动同步,代码更简洁、可维护性更好。
四、完整示例
将资源与绑定组合到一个窗口中:
<Window x:Class="_3绑定值.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:vm="clr-namespace:_3绑定值.Model"
Title="资源绑定与数据绑定" Height="450" Width="400">
<Window.Resources>
<sys:String x:Key="str1">成功</sys:String>
<x:Array x:Key="array1" Type="sys:String">
<sys:String>坚果</sys:String>
<sys:String>蘑菇</sys:String>
<sys:String>木瓜</sys:String>
</x:Array>
<x:Array x:Key="array2" Type="sys:Int32">
<sys:Int32>1</sys:Int32>
<sys:Int32>2</sys:Int32>
<sys:Int32>3</sys:Int32>
</x:Array>
<SolidColorBrush x:Key="s1" Color="Red"/>
<x:Array x:Key="s2" Type="Color">
<Color>Red</Color>
<Color>Green</Color>
<Color>Blue</Color>
</x:Array>
</Window.Resources>
<Window.DataContext>
<vm:People></vm:People>
</Window.DataContext>
<StackPanel>
<ListBox Name="listbox" Width="100" Height="100"
SelectionChanged="listbox_SelectionChanged"/>
<TextBlock Text="{StaticResource str1}"/>
<ListBox Width="100" Height="100" ItemsSource="{StaticResource array1}"/>
<ListBox Width="100" Height="100" ItemsSource="{StaticResource array2}"/>
<ListBox Width="100" Height="100" ItemsSource="{StaticResource array2}"
Background="{StaticResource s1}"/>
<ListBox Width="100" Height="100" ItemsSource="{StaticResource s2}"
Background="{StaticResource s1}"/>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Age}"/>
<ListBox Width="100" Height="100" ItemsSource="{Binding list}"
Background="{StaticResource s1}"/>
</StackPanel>
</Window>
五、总结
- 资源定义与使用:在
<Window.Resources>中集中定义字符串、数组、颜色、画刷等资源,并通过x:Key命名;使用时用{StaticResource key 名}引用,XAML 加载时一次性查找赋值,适合加载后不再变化的资源。 - DataContext + Binding 绑定流程:① 定义实体类;② 引入实体类所在命名空间
xmlns:vm="clr-namespace:命名空间";③ 通过<Window.DataContext>指定上下文对象;④ 用{Binding 属性名}把数据绑定到界面元素。 - 资源与绑定组合的优势:资源负责界面外观与静态数据的复用,绑定负责数据与界面的自动同步,二者结合可减少后台代码中的手动赋值,让界面与数据保持一致,代码更简洁、可维护性更好。
- 实践建议:初学者可先从一个字符串资源和一个
{Binding}属性入手,跑通后再逐步加入数组、画刷等资源,理解“定义 → 引用 → 绑定”这条主线。
转载自 CSDN-专业IT技术社区
原文链接:https://blog.csdn.net/weixin_58307908/article/details/171049236



