一、WPF应用程序和WPF应用(.NET Framework)区别
WPF应用程序:是依赖项
.NET Framework:是引用,利用NuGet进行添加
二、事件驱动和数据驱动
事件驱动:例如按钮,点击按钮事件响应,在事件中处理逻辑
数据驱动:
- 通过建立数据对象,对象继承INotifyPropertyChanged订阅发布到页面
- 页面通过{Binding Value}绑定数据,后台逆向传值、委托this.DataContext = m获取数据
- 利用PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(“Value”));页面订阅了实例就可以更新
三、功能字段说明
多行注释:Ctrl + K,Ctrl + C
取消多行注释:Ctrl + K,Ctrl + U
构建get\set:propfull + 两次Tab
构造函数:ctor
Margin左上右下
HorizontalAlignment水平
VerticalAlignment垂直
Foreground字体颜色
四、代码
数据对象
public class MainViewModel:INotifyPropertyChanged
{
public MainViewModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
private string _value = "ooo";
public string Value
{
get { return _value; }
set
{
_value = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
if (value == "100")
{
ValueColor = Brushes.Red;
}
}
}
private Brush _valueColor = Brushes.Orange;
public Brush ValueColor
{
get { return _valueColor; }
set {
_valueColor = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ValueColor"));
}
}
}
页面
<Grid>
<Button x:Name="login" Content="登录" HorizontalAlignment="Left" Margin="498,85,0,0" VerticalAlignment="Top" Height="28" Width="70" Click="login_Click"/>
<TextBlock Text="用户名" HorizontalAlignment="Left" Margin="219,90,0,0" VerticalAlignment="Top" Width="110" Height="33"/>
<TextBox x:Name="DbName" HorizontalAlignment="Left" Margin="306,90,0,0" TextWrapping="Wrap"
Text="{Binding Value}" Foreground="{Binding ValueColor}" VerticalAlignment="Top" Width="120" RenderTransformOrigin="-0.92,-0.385"/>
</Grid>
页面后台
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainViewModel m = null;
public MainWindow()
{
InitializeComponent();
m = new MainViewModel();
this.DataContext = m;
}
private void login_Click(object sender, RoutedEventArgs e)
{
//string dbName = this.DbName.Text;
//this.DbName.Text = "111";
//if (dbName.Equals("111"))
//{
// this.DbName.Foreground = Brushes.Orange;
//}
m.Value = "100";
}
}