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