<转>WPF 中的绑定

在WPF应用的开发过程中Binding是一个非常重要的部分。

在实际开发过程中Binding的不同种写法达到的效果相同但事实是存在很大区别的。

这里将实际中碰到过的问题做下汇总记录和理解。

1. source = {binding} 和source = {binding RelativeSource={RelativeSource self},Path=DataContext}效果相同

理解:{binding} 不设定明确的绑定的source,这样binding就去从本控件类为开始根据可视树的层次结构自下而上查找不为空的Datacontext属性的值。

{binding RelativeSource={RelativeSource self},Path=DataContext}中RelativeSource self的含义为绑定的source为控件自身,这样binding 就绑定了自身控件的Datacontext。

效果:

<StackPanel DataContext="abc">         <Label Content="{Binding}"></Label>         <Label Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>     </StackPanel>

<StackPanel DataContext="abc">         <Label Content="{Binding}"></Label>         <Label DataContext="def" Content="{Binding RelativeSource={RelativeSource Self},Path=DataContext}"></Label>     </StackPanel>

2.在Template的Trigger中改变Template中某个样式控件的属性

<Style TargetType="{x:Type Button}">         <Setter Property="Template">             <Setter.Value>                 <ControlTemplate TargetType="{x:Type Button}">                     <Border>                         <Label x:Name="PART_Label" Content="{TemplateBinding ContentA}" />                     </Border>

<ControlTemplate.Triggers>                         <Trigger Property="IsChecked" Value="True">                         注:    <Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource TemplatedParent}}" />                         </Trigger>                     </ControlTemplate.Triggers>                 </ControlTemplate>                            </Setter.Value>         </Setter>     </Style>

当然把注:的这句改成<Setter TargetName="PART_Label" Property="Content" Value="{Binding Path=ContentB, RelativeSource={RelativeSource AncestorType={x:Type Button}}}">效果一样。

我们来看一看Elementname,Source,RelativeSource 三种绑定的方式

1.ElementName顾名思义就是根据Ui元素的Name来进行绑定:

例子:

<Window x:Name="MainWindow">

<Grid>                <Button Background=”{Binding ElementName=MainWindow, Path=Background}”/>          </Grid>

</Window>

效果等同于

<Window>

<Grid>                <Button Background=”{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window},Path=Background}”/>          </Grid>

</Window>

区别:

ElementName属性用于引用一个UI对象的名称,其的作用域在同一XAML文件内,不能引用另一XAML文件的某个Ui元素名。

2.Source属性用于指定对象绑定路径的引用。 其特点是:Source属性通常用于绑定设置的对象时,是已知的。

<Window x:Name="MainWindow">

<Grid>                <Button Background=”{Binding Source={StaticResource ButtonStyle}}”/>          </Grid>

</Window>

3.RelativeSource

在不确定绑定的Source时,但知道与绑定对象两者相对关系时就需要使用RelativeSource,这也是RelativeSource 与ElementName和Source的最大区别。

RelativeSource 的三种典型用法:

/1.UI元素的一个属性绑定在自身的另一个属性上

<Label Background = {Binding Path=Forgroud, RelativeSource={RelativeSource Self}} />

/2.UI元素的一个属性绑定在某个父元素的属性上

<Grid>

<Label Background = {Binding Path=Background, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}/>

</Grid>

/3.Template中的元素的属性绑定在Template使用者元素的属性上

{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}

例子:

<Style TargetType="{x:Type local:NumericUpDown}">   <Setter Property="HorizontalAlignment" Value="Center"/>   <Setter Property="VerticalAlignment" Value="Center"/>   <Setter Property="Template">     <Setter.Value>       <ControlTemplate TargetType="{x:Type local:NumericUpDown}">         <Grid Margin="3">           <Grid.RowDefinitions>             <RowDefinition/>             <RowDefinition/>           </Grid.RowDefinitions>           <Grid.ColumnDefinitions>             <ColumnDefinition/>             <ColumnDefinition/>           </Grid.ColumnDefinitions>           <Border BorderThickness="1" BorderBrush="Gray"                   Margin="2" Grid.RowSpan="2"                   VerticalAlignment="Center" HorizontalAlignment="Stretch">

<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Value}"                        Width="60" TextAlignment="Right" Padding="5"/>           </Border>         </Grid>       </ControlTemplate>     </Setter.Value>   </Setter> </Style>

利用TemplateBinding 绑定模板与原对象之间的属性

{TemplateBinding Path=PathToProperty}

例子:

<ControlTemplate TargetType="{x:Type Button}"  x:Key="buttonTemp">                                <Border BorderThickness="3" Background="{TemplateBinding Foreground}">                              <TextBlock Foreground="{TemplateBinding Background}"/>                            </Border>                       </ControlTemplate>

时间: 2024-08-01 06:44:36

<转>WPF 中的绑定的相关文章

ComboBox在WPF中的绑定示例:绑定项、集合、转换,及其源代码

在WPF的Xaml中为ComboBox绑定数据时,由于参数很多,很容易混淆,在ListView中使用更是如此.本文通过对ComboBox在窗口和在ListView中绑定对象的属性和属性可能是枚举类型的情况进行简单讲解和示例,以作实际应用参照. 源码可以到这里下载:ComboBoxBindings.rar 1.ComboBox在窗口容器中的情况 2.ComboBox在ListView中的情况 3.绑定枚举 示例中做枚举类型Sex的绑定时,先在Xaml中绑定值,然后在ComboBox的ItemsSo

WPF 中的绑定方式

1.元素间的绑定 xaml方式 <Slider Name="slider1" Value="20"/>        <TextBlock TextWrapping="Wrap" FontSize="20" Text="{Binding ElementName=slider1,Path=Value,Mode=TwoWay}"/> 代码方式 Student stu = new Stud

WPF中Grid绑定DataTable数据。

1.首先引用DocumentFormat.OpenXml.dll 2.然后新建一个OpenExcelHelper类,将Excel转化为Datatable. /// <summary>        /// 根据指定EXCEL流组织成DataTable        /// </summary>        /// <param name="sheetName">需要读取的SHEET</param>        /// <para

WPF中ComboBox绑定数据库自动读取产生数据

前台端 <ComboBox HorizontalAlignment="Left" Margin="410,113,0,0" VerticalAlignment="Top" Width="300"Name="cmb_SSBM" DisplayMemberPath="NAME" SelectedValuePath="CODE" SelectedIndex="

wpf中如何在xaml中绑定cs中类的属性

cs代码:/// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); ContentGrid.DataContext = this; this.Path = "数据绑定"; } public string Path { get; set;

MVVM模式解析和在WPF中的实现(三) 命令绑定

MVVM模式解析和在WPF中的实现(三) 命令绑定 0x00 命令绑定要达到的效果 命令绑定要关注的核心就是两个方面的问题,命令能否执行和命令怎么执行.也就是说当View中的一个Button绑定了ViewModel中一个命令后,什么时候这个Button是可用的,按下Button后执行什么操作.解决了这两个问题基本就实现了命令绑定.另外一个问题就是执行过程中需要的数据(参数)要如何传递.本次主要探讨这几个问题. 0x01 命令绑定的实现 自定义一个能够被绑定的命令需要实现ICommand接口.该接

转:WPF中ListBox的创建和多种绑定用法

先从最容易的开始演示ListBox控件的创建. Adding ListBox Items下面的代码是向ListBox控件中添加多项ListBoxItem集合.XAML代码如下:<ListBox Margin="10,10,0,13" Name="listBox1" HorizontalAlignment="Left"         VerticalAlignment="Top" Width="194"

MVVM设计模式和在WPF中的实现(四) 事件绑定

系列目录: MVVM模式解析和在WPF中的实现(一)MVVM模式简介 MVVM模式解析和在WPF中的实现(二)数据绑定 MVVM模式解析和在WPF中的实现(三)命令绑定 MVVM模式解析和在WPF中的实现(四)事件绑定 MVVM模式解析和在WPF中的实现(五)View和ViewModel的通信 MVVM模式解析和在WPF中的实现(六)用依赖注入的方式配置ViewModel并注册消息 0x00 为什么要事件绑定 这个问题其实是很好理解的,因为事件是丰富多样的,单纯的命令绑定远不能覆盖所有的事件.例

WPF中的命令与命令绑定导航

1.WPF中的命令与命令绑定(一) (引入命令) 2.WPF中的命令与命令绑定(二)(详细介绍命令和命令绑定) WPF中的命令与命令绑定导航