我见过类似的问题,但我仍然无法满足我的需求。我需要输出复选框的名称通过标签的用户控件中:如何从用户控件绑定到父控件的属性?
Window1.xaml:
<Window x:Class="WpfBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfBinding" Title="Window1" Height="300" Width="300">
<Grid>
<CheckBox Name="checkBox1">
<local:UserControl1></local:UserControl1>
</CheckBox>
</Grid>
</Window>
UserControl1.xaml:
<UserControl x:Class="WpfBinding.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas>
<Label Content="{Binding ElementName=checkBox1, Path=Name}"></Label>
</Canvas>
</UserControl>
如何正确做呢?我缺乏什么知识?感谢帮助。
你为什么要这样做?你可以在你的UserControl中创建一个获得父控件名称或者其他值的属性。 – Clemens
如果我在代码隐藏方面做到这一点,那么查看xaml标记就不会轻易被注意到。这就是为什么我需要一种XAML方式。 – despero