日期:2014-05-17  浏览次数:20840 次

WPF 数据绑定

<Button x:Name="button1" >
            <Button.Template>
                <ControlTemplate>
                    <Grid>
                        <TextBox Height="57"  HorizontalAlignment="Left" Margin="46,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="190" FontSize="40" />
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>

        <Button Name="button2" Content="Button" Height="23" HorizontalAlignment="Left" Margin="108,176,0,0"  VerticalAlignment="Top" Width="75" Click="button1_Click" />

如何将 textBox1 的Text属性绑定到button2的content属性上
wpf

------解决方案--------------------
       <Button x:Name="button1">
            <Button.Template>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <TextBox Height="57"  Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Content}"
                                 HorizontalAlignment="Left" Margin="46,12,0,0" VerticalAlignment="Top" Width="190" FontSize="40" />
                    </Grid>
                </ControlTemplate>
            </Button.Template>
        </Button>
        <Button Name="button2" Content="{Binding ElementName=button1,Path=Content}" Height="23" HorizontalAlignment="Left" Margin="108,176,0,0"  VerticalAlignment="Top" Width="75" />

------解决方案--------------------

Content="{Binding ElementName=button1,Path=Content,UpdateSourceTrigger=PropertyChanged}" 

引用:
谢谢了,还有个问题就是,改变textbox的内容的时候,button上面的内容没有立即改变,而是点击button过后button上面的内容才刷新,不知都这是什么原因

------解决方案------------------