本文会持续更新,发现新的绑定技术点同步更新
1.前后端简单绑定
第一种比较常见,常见于mvvm框架
前端
<TextBlock Text="{Binding Path=Name}"></TextBlock>
后端
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));
}
}
有一个比较重要的点就是,继承INotifyPropertyChanged,并实现OnPropertyChanged
2.converter转换绑定
从业wpf一段时间之后发现,特别是自学wpf的人,会漏掉这个技术点,这里补充一下。
前端
<Cvts:NameToUper x:Key="NameToUper"/>
<TextBlock Text="{Binding Path=Name, Converter={StaticResource NameToUper}}"></TextBlock>
后端
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));
}
}
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;
}
}
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>
4.x:Reference 绑定
开发的时候会意见一些比较特殊的情况,内嵌的控件要绑定ViewModel的数据。用直接的绑定时不行的,要用代理
{Binding Path=DisplayColumnIsChanged,Source={x:Reference Name=model},Converter={StaticResource DetailVisibleConverter}
5.静态绑定
很多固化的信息,不需要写在viewmodel里面,直接写在静态类就可以
Content="{Binding Path=MESState, Source={x:Static Station:Station.Current},Converter={StaticResource BoolToVisibleCvt}}"
还有的时候,Datacontext也不一定要这么写
<Window.DataContext>
<model:SimpleModel/>
</Window.DataContext>
也可以这么写,静态绑定,也方便其它地方调用
<Window.DataContext>
<Binding Source="{x:Static Station:Station.Current}"></Binding>
</Window.DataContext>
或者
DataContext="{Binding Source={x:Static vm:MainWindowViewModel.Instance}}"