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

本文会持续更新,发现新的绑定技术点同步更新

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}}"
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值