WPF绑定方式

 

绑定到其它元素


<Grid> 
    <StackPanel> 
        <TextBox x:Name="textbox1" /> 
        <Label Content="{Binding ElementName=textbox1, Path=Text}" /> 
    </StackPanel>
</Grid>
绑定元素事件

 <i:Interaction.Triggers>  

<i:EventTrigger EventName="Loaded">  

<i:InvokeCommandAction Command="{Binding LoadedCommand}" CommandParameter="{Binding ElementName=window}" />  

</i:EventTrigger>  

</i:Interaction.Triggers>  

 

public ICommand LoadedCommand {
get {
       return new DelegateCommand<Window>(win => {
      win.Closing += (sender, e) => {
      if (MessageBox.Show("确认要关闭窗口吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) {
      e.Cancel = true;
}
};
});
}
}

绑定到静态资源
<Window.Resources> 
        <ContentControl x:Key="text">Hello, World!</ContentControl>
</Window.Resources>
<Grid> 
    <StackPanel> 
        <Label x:Name="label1" Content="{Binding Source={StaticResource text}}" /> 
    </StackPanel>
</Grid>
<STRONG>3. 绑定到自身</STRONG>
<Grid> 
    <StackPanel> 
        <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource Self}, Path=Name}" /> 
    </StackPanel>
</Grid>
绑定到指定类型的父元素
<Grid x:Name="Grid1"> 
        <StackPanel> 
            <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,  
                AncestorType={x:Type Grid}}, Path=Name}" /> 
        </StackPanel>
</Grid>
 
绑定到对象
public class Person  
   {  
       public string Name { get; set; }  
       public int Age { get; set; }  
   }
<StackPanel x:Name="stackPanel">  
        <StackPanel.DataContext>  
            <local:Person Name="Jack" Age="30"></local:Person>  
        </StackPanel.DataContext>  
        <TextBlock  Text="{Binding Path=Name}"></TextBlock>  
        <TextBlock Text="{Binding Path=Age}"></TextBlock>         
</StackPanel>


绑定到集合
public class Person  
    {  
        public string Name { get; set; }  
        public int Age { get; set; }  
    } 


public class PersonList : ObservableCollection<Person>  
    { }
<Window.Resources>  
       <local:PersonList x:Key="person">  
           <local:Person Name="Jack" Age="30"></local:Person>  
           <local:Person Name="Tom" Age="32"></local:Person>  
       </local:PersonList>  
   </Window.Resources>  
   <StackPanel x:Name="stackPanel">  
       <ListBox  ItemsSource="{Binding Source={StaticResource ResourceKey=person}}" 
                  DisplayMemberPath="Name">             
       </ListBox> 
</StackPanel>
 
DataContext共享源
我们需要将同一资源绑定到多个 UI 元素上,很显然到处写 "{Binding Source={StaticResource person}}" 是件很繁琐且不利于修改的做法。WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。
<Window.Resources>  
        <local:PersonList x:Key="person">  
            <local:Person Name="Jack" Age="30"></local:Person>  
            <local:Person Name="Tom" Age="32"></local:Person>  
        </local:PersonList>  
    </Window.Resources>  
    <StackPanel x:Name="stackPanel" DataContext="{StaticResource person}">  
        <ListBox  ItemsSource="{Binding}" 
                   DisplayMemberPath="Name">             
        </ListBox>  
    </StackPanel>
 
使用XML作为Binding的源
XML:
<?xml version="1.0" encoding="utf-8" ?> 
<PersonList>  
  <Person Id="1">  
    <Name>Jack</Name>  
  </Person>  
  <Person Id="2">  
    <Name>Tom</Name>  
  </Person>  
  <Person Id="3">  
    <Name>Justin</Name>  
  </Person>  
  <Person Id="4">  
    <Name>David</Name>  
  </Person> 
</PersonList>

 


XAML:
<StackPanel>  
       <ListView x:Name="personListView">  
           <ListView.View>  
               <GridView>  
                   <GridViewColumn Header="Id" Width="100" 
                                    DisplayMemberBinding="{Binding XPath=@Id}"/>  
                   <GridViewColumn Header="Name" Width="100" 
                                    DisplayMemberBinding="{Binding XPath=Name}"/>  
               </GridView>  
           </ListView.View>  
       </ListView>     
       <Button Click="Button_Click">Load Data</Button>  
   </StackPanel>
 

private void Button_Click(object sender, RoutedEventArgs e)  
 {  
            XmlDocument xmlDocument = new XmlDocument();  
            xmlDocument.Load("Person.xml"); 
            XmlDataProvider xdp = new XmlDataProvider();  
            xdp.Document = xmlDocument;  
            xdp.XPath = @"/PersonList/Person"; 
  
            this.personListView.DataContext = xdp;  
            this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());  
        }

 

XAML中的绑定语句,这里用ElementName就是制定要绑定的对象的名字,Path就是要绑定的依赖项属性,mode就是绑定方式,有几种方式:
(1)OneWay
(2)TwoWay
(3)OneTime,最初根据源属性值设置目标属性,以后就忽略所有改变,就是说,只进行初始化。
(4)OneWayToSource,这和OneWay相反
(5)Default,这是默认形式,它根据目标属性自动设置。
但是,如果把TextBox中的值修改成其他的,滑条位置没有改变,字体大小也没有改变,这是什么回事呢?当TextBox失去焦点的时候,就会发生相应的改变了。这是因为这个绑定中的默认更新机制,更新机制Binding.UpdateSourceTrigger,这个属性有4个枚举值
(一)PropertyChange,当值改变的时候,就更新。
(二)LostFocus,当时去焦点的时候更新.
(三)Explicit,当调用BingingExpression.UpdateSource()方法的使用更新,其他情况不会更新。
(四)Default,默认形式
可以参考这篇文章:  http://blog.sina.com.cn/s/blog_7b60d05f0101oqn7.html
posted @ 2016-09-13 09:12  jeffery1010  Views(1072)  Comments(0Edit  收藏  举报