读书人

WPF 数据绑定解决办法

发布时间: 2013-04-12 18:33:11 作者: rapoo

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上面的内容才刷新,不知都这是什么原因

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


不是依赖项属性 不能即时刷新
[解决办法]

引用:
引用:不是依赖项属性 不能即时刷新
Content 肯定是依赖属性。


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

引用 2 楼 asker2001……




Content是依赖项属性 但是也要保证你绑定到Content的TextBox的Text是依赖项属性! 试试把TextBox的Text转换成依赖项属性在绑定 试下应该能即时刷新
[解决办法]
引用:
引用:不是依赖项属性 不能即时刷新
Content 肯定是依赖属性。


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

引用 2 楼 asker2001……

Text="{Binding RelativeSource={RelativeSource Mode=TemplatedParent},Path=Content}"
这个加上UpdateSourceTrigger=PropertyChanged
[解决办法]
引用:
引用:不是依赖项属性 不能即时刷新
Content 肯定是依赖属性。


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

引用 2 楼 asker2001……



参考 http://blog.csdn.net/datoumimi/article/details/2874573
这篇文章里面有个Name属性和你这儿的Text属性类似

读书人网 >C#

热点推荐