什么是Fody?
它是一款可以帮助你节省实现数据通知( INotifyPropertyChanged )的开源组件,并且不用在每次需要属性通知的时候又写私有字段、又写属性封装操作节省很多代码量使你的代码更加整洁清爽。
1.Nuget下载Fody组件
2.配置FodyWeavers.xml
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<PropertyChanged />
<Costura/>
</Weavers>
- 编写model
using PropertyChanged;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp2_Fody
{
class FodyModel : INotifyPropertyChanged
{
public string ID { get; set; }
public string Name { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}
}
4.前端xaml
<Window x:Class="WpfApp2_Fody.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:WpfApp2_Fody"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel>
<TextBox Text="{Binding ID, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</Grid>
</Window>
- .cs绑定model
- 运行结果