日期:2014-05-16  浏览次数:21225 次

WPF,这个DataContext的绑定怎么写?
<StackPanel DataContext="button1">  
    <TextBlock Text="{Binding Path=Content}" />
    <TextBlock Text="{Binding Path=Content}" />
    <TextBlock Text="{Binding Path=Content}" />
    ...
    ...
</StackPanel>
<Button Name="button1" Content="张三" />


上面的StackPanel内有很多TextBlock,Text都绑定到button1的Content,那么,如果要用DataContext来指定绑定源的话,DataContext="button1"这种写法正确吗?

------解决方案--------------------
<TextBlock Text="{Binding ElementName=butten1, Path=Content}" />
------解决方案--------------------
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <TextBlock Text="{Binding  ElementName=button1, Path=Content}" />
            <TextBlock Text="{Binding  ElementName=button1, Path=Content}" />
            <TextBlock Text="{Binding  ElementName=button1, Path=Content}" /> 
            <Button Name="button1" Content="张三" />
        </StackPanel>
     
    </Grid>
</Window>

------解决方案--------------------
        <Button Name="button1" Content="张三" HorizontalAlignment="Left" Margin="0,274,0,0" VerticalAlignment="Top" Width="75"/>
        
        <StackPanel HorizontalAlignment="Left" Height="100" Margin="102,196,0,0" VerticalAlignment="Top" Width="259" DataContext="{Binding Content, ElementName=button1}">
            <TextBlock Text="{Binding}" />
            <TextBlock Text="{Binding}" />
            <TextBlock Text="{Binding}" />
        </StackPanel>