首页 > 其他分享 >WPF开发01-数据绑定的几种方式,静态,动态、向上查找、适应各种情况

WPF开发01-数据绑定的几种方式,静态,动态、向上查找、适应各种情况

时间:2024-05-27 23:23:02浏览次数:15  
标签:01 Name 绑定 value name WPF public string

1.前后端简单绑定
第一种比较常见,常见于mvvm框架
前端

<TextBlock Text="{Binding Path=Name}"></TextBlock>
1
后端

public class PersonViewModel : INotifyPropertyChanged
{
public string Name
{
get { return name; }
set
{
if (name != value)
{
person.Name = value;
OnPropertyChanged("Name");
}
}
}
private string name;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
有一个比较重要的点就是,继承INotifyPropertyChanged,并实现OnPropertyChanged

2.converter转换绑定
从业wpf一段时间之后发现,特别是自学wpf的人,会漏掉这个技术点,这里补充一下。
前端

<Cvts:NameToUper x:Key="NameToUper"/>
<TextBlock Text="{Binding Path=Name, Converter={StaticResource NameToUper}}"></TextBlock>
1
2
后端

public class PersonViewModel : INotifyPropertyChanged
{
public string Name
{
get { return name; }
set
{
if (name != value)
{
person.Name = value;
OnPropertyChanged("Name");
}
}
}
private string name;

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
NameToUper转换器代码

public class NameToUper:IValueConverter
{

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value.ToString().ToUper();
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
3.FindAncestor绑定
下面这个例子是实现菜单选中的样式,两层的listview做两级菜单

<ListView x:Name="listView" ItemsSource="{Binding list}" BorderThickness="0" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<Border>
<StackPanel>
<TextBox Text="{Binding Content}"/>
<ListView Margin="10 0 0 0" ItemsSource="{Binding SubQuesInfos}" SelectionMode="Extended"
Visibility="{Binding IsSelected, Converter={StaticResource ItemsPanelStyleCvt},
Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}}">
<ListView.ItemTemplate>
<DataTemplate>
<Border>
<StackPanel>
<TextBlock Text="{Binding Content}"/>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Style>
<Style TargetType="{x:Type ListView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="1" SnapsToDevicePixels="true">
<StackPanel Focusable="false">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</StackPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.Style>
</ListView>
</StackPanel>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
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
4.x:Reference 绑定
开发的时候会意见一些比较特殊的情况,内嵌的控件要绑定ViewModel的数据。用直接的绑定时不行的,要用代理

{Binding Path=DisplayColumnIsChanged,Source={x:Reference Name=model},Converter={StaticResource DetailVisibleConverter}
1
5.静态绑定
很多固化的信息,不需要写在viewmodel里面,直接写在静态类就可以

Content="{Binding Path=MESState, Source={x:Static Station:Station.Current},Converter={StaticResource BoolToVisibleCvt}}"
1
还有的时候,Datacontext也不一定要这么写

<Window.DataContext>
<model:SimpleModel/>
</Window.DataContext>
1
2
3
也可以这么写,静态绑定,也方便其它地方调用

<Window.DataContext>
<Binding Source="{x:Static Station:Station.Current}"></Binding>
</Window.DataContext>
1
2
3
或者

DataContext="{Binding Source={x:Static vm:MainWindowViewModel.Instance}}"
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/magicchz/article/details/129117200

标签:01,Name,绑定,value,name,WPF,public,string
From: https://www.cnblogs.com/webenh/p/18216826

相关文章

  • Wpf经验技巧-使用 d:DataContext 指定 DataContext 的类型.
    VM代码:V代码(版本1):没有指定DataContext的类型,所以下面的绑定并不知道P1和P3到底是什么,也就无法在代码编辑时检测出绑定是否正确.如果写错了,只能等到程序运行并打开这个窗口时报错才能知道.V代码(版本2):通过d:DataContext指定了DataContext的类型,所以下面的绑定可以知道......
  • 一步一步实现WPF透明化窗口
    这一篇教程讲述如何实现透明窗体和透明控件,在WindowStyle设置为none情况下拖拽窗口,半透明作为较容易实现的一种美观化,对于大多数美工较弱的开发者来说实用性不错,能在一些平面化设计场合发挥简单而有效的美化效果。  实现效果1:窗体整体半透明   实现效果2:窗体全透明......
  • WPF设置Button的Style
    扣扣技术交流群:460189483一、前言程序界面上的按钮多种多样,常用的就这几种:普通按钮、图标按钮、文字按钮、图片文字混合按钮。本文章记录了不同样式类型的按钮实现方法。下面话不多说了,来一起看看详细的介绍吧。二、固定样式的按钮固定样式的按钮一般在临时使用时或程序的样式......
  • WPF在ListView中绑定Command命令的写法
    假定:ViewModel中有一个数据源叫Persons,有一个命令叫DoCommand,通过System.Windows.Interactivity触发器绑定鼠标MouseUp事件,当UI端绑定了DataContext数据上下文之后,Command="{BindingDoCommand}"是找不到这个命令的,必须使用Binging类的RelativeSource属性先找到当前UI,再找到DataC......
  • WPF之单例模式
    项目2019/10/09 问题2019年10月9日星期三上午2:461、为了实现单例模式,在App类中添加了如下代码,使用了信号量,但是为什么返回;isNew一直为truepublicpartialclassApp:Application   {       protectedoverridevoidOnStartup(StartupEventArgs......
  • WPF DataGrid使用 自动显示行号、全选、三级联动、拖拽
    1.DataGrid的使用自动显示行号(修复删除行时行号显示不正确)  ViewCodedgTool.LoadingRow+=newEventHandler<DataGridRowEventArgs>(dgTool_LoadingRow);dgTool.UnloadingRow+=newEventHandler<DataGridRowEventArgs>(dgTool_UnloadingRow);voi......
  • WebView2在WPF中的应用
    开发环境运行环境:.Net6开发环境:VisualStudio202217.1.3框架语言:WPF安装WebView2通过PackageManager控制台安装Install-PackageMicrosoft.Web.WebView2通过Nuget包管理器安装在窗体中添加名字空间:xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;asse......
  • [博客迁移20190713]题解 P4169 【[Violet]天使玩偶/SJY摆棋子】
    《算法竞赛》书上例题(可惜原书没代码)天使玩偶,一道好题。(书p243)我就来谈谈自己的想法吧!而总有人在这种明明可以离线处理的三维偏序问题上投机取巧。如:KDtree。蒟蒻想说,KDtree在这题复杂度是不对的。虽有剪枝,可是还是有可能遍历整棵树的(期望复杂度不靠谱)对上述看法有争议的,请跳......
  • [NOIP2001 提高组] 一元三次方程求解
    题目描述形如: 这样的一个一元三次方程。给出该方程中各项的系数(......
  • [NOIP 2014] 寻找道路
    [NOIP2014]寻找道路在有向图 G 中,每条边的长度均为 11,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件:路径上的所有点的出边所指向的点都直接或间接与终点连通。在满足条件 11 的情况下使路径最短。注意:图 G 中可能存在重边和自环,题目保证......