zoukankan      html  css  js  c++  java
  • Binding ,抄自 http://www.cnblogs.com/cnblogsfans/archive/2011/02/19/1958586.html

    1. 绑定到其它元素

     

    <Grid>
    <StackPanel >
    <TextBox x:Name="textBox1" Height="150" BorderBrush="Black" BorderThickness="10"/>
    <Label Content="{Binding ElementName=textBox1,Path=Text}"/>
    </StackPanel>

    </Grid>

    2. 绑定到静态资源

    <Window.Resources>
    <ContentControl x:Key="FirstKey">Hello World!</ContentControl>
    </Window.Resources>
    <Grid>
    <StackPanel>
    <Label x:Name="label1" Content="{Binding Source={StaticResource ResourceKey=FirstKey}}"/>
    </StackPanel>
    </Grid>

    3.绑定到自身

    <StackPanel>
    <Label x:Name="label45347356736" BorderBrush="Black" Width="1200" Content="{Binding RelativeSource={RelativeSource Self},Path=Width}"/>
    </StackPanel>

    4.绑定到指定类型的父元素

     

    <Grid x:Name="Grid1">
    <StackPanel>
    <Label x:Name="label1" Content="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type Grid}},Path=Name}"/>
    </StackPanel>
    </Grid>

    5. 绑定到对象 

    public class Person
    {
    public string Name { get; set; }
    public int Age { get; set; }
    }

    <StackPanel x:Name="stackPanel">
    <StackPanel.DataContext>
    <local:Person Name="Fred" Age="29"/>
    </StackPanel.DataContext>
    <TextBlock Text="{Binding Path=Name}"/>
    <TextBlock Text="{Binding Path=Age}"/>
    </StackPanel>

    6. 绑定到集合

    <Window.Resources>
    <local:PersonList x:Key="person">
    <local:Person Name="Fred" Age="30"/>
    <local:Person Name="ZFF" Age="30"/>
    </local:PersonList>
    </Window.Resources>
    <StackPanel x:Name="stackPanel">
    <ListBox ItemsSource="{Binding Source={StaticResource ResourceKey=person}}" DisplayMemberPath="Name"/>
    </StackPanel>

    public class Person
    {
    public string Name { get; set; }
    public int Age { get; set; }
    }

    public class PersonList:ObservableCollection<Person>
    {

    }

    8.DataContext共享源

    WPF 提供了一个称之为 "数据上下文 (DataContext)" 的东西让我们可以在多个元素上共享一个源对象,只需将其放到父元素 DataContext 属性即可。当我们不给 Binding 扩展标志指定 Source 属性时,它会自动寻找上级父元素的数据上下文。

     9.使用XML作为Binding的源
     
    <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 btn_Click(object sender, RoutedEventArgs e)
    {
    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.Load(@"E:Work20150516WpfApplication368WpfApplication368FirstXML.xml");

    XmlDataProvider xdp = new XmlDataProvider();
    xdp.Document = xmlDocument;
    xdp.XPath = @"/PersonList/Person";

    this.personListView.DataContext = xdp;
    this.personListView.SetBinding(ListView.ItemsSourceProperty, new Binding());
    }

     
  • 相关阅读:
    2018-2019-120165226_20165310_20165315 实验四 外设驱动程序设计
    20165310 实验三-并发程序
    Exp2_固件程序设计 20165226_20165310_20165315
    实验楼缓冲区溢出实验报告
    2018-2019-2 《网络对抗技术》Exp4 恶意代码分析20165211
    2018-2019-2 《网络对抗技术》Exp3 免杀原理与实践 20165211
    2018-2019-2 《网络对抗技术》Exp2 后门原理与应用 20165211
    2018-2019-2 《网络对抗技术》Exp1 PC平台逆向破解 Week3 20165211
    2018-2019-2 《网络对抗技术》Exp0 Kali安装 Week1 20165211
    2018-2019-1 20165211 实验四 外设驱动程序设计
  • 原文地址:https://www.cnblogs.com/Fred1987/p/5978120.html
Copyright © 2011-2022 走看看