为什么数据没有绑定到我的WPF应用程序中的标签

时间:2013-06-05 06:23:51

标签: c# .net wpf visual-studio-2010

我正在DataBinding中执行简单的WPF申请。在这个TextBox中我输入的任何名为txtName的内容都应该在Label上得到。为此,我在WPF中使用了绑定。

但它没有受到约束。

我的XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="315*" />
        <ColumnDefinition Width="188*" />
    </Grid.ColumnDefinitions>

    <TextBox Height="23" HorizontalAlignment="Left" Margin="79,12,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
    <Label Height="28" HorizontalAlignment="Left" Margin="22,12,0,0" Name="label1" VerticalAlignment="Top" Width="42" Content="Name"/>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="79,56,0,0" Name="txtSirName" VerticalAlignment="Top" Width="120" />
    <Label Content="SirName" Height="28" HorizontalAlignment="Left" Margin="12,56,0,0" Name="label2" VerticalAlignment="Top" Width="52" />

    <!-- Following is the label which i bound -->

    <Label Content="{Binding ElementName=txtName,Path=text}" Height="28" HorizontalAlignment="Left" Margin="47,112,0,0" Name="label3" VerticalAlignment="Top" Width="171" />
</Grid>

我是WPF新手。请指导我。

4 个答案:

答案 0 :(得分:7)

非常确定XAML区分大小写。当您的绑定路径为text时,您的绑定路径为Text

答案 1 :(得分:1)

TextBox具有属性Text,而不是文本。小错误:))

 <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="315*" />
                <ColumnDefinition Width="188*" />
            </Grid.ColumnDefinitions>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="79,12,0,0" Name="txtName" VerticalAlignment="Top" Width="120" />
            <Label Height="28" HorizontalAlignment="Left" Margin="22,12,0,0" Name="label1" VerticalAlignment="Top" Width="42" Content="Name"/>
            <TextBox Height="23" HorizontalAlignment="Left" Margin="79,56,0,0" Name="txtSirName" VerticalAlignment="Top" Width="120" />
            <Label Content="SirName" Height="28" HorizontalAlignment="Left" Margin="12,56,0,0" Name="label2" VerticalAlignment="Top" Width="52" />
            <Label Content="{Binding ElementName=txtName,Path=Text}" Height="28" HorizontalAlignment="Left" Margin="47,112,0,0" Name="label3" VerticalAlignment="Top" Width="171" />
</Grid>

答案 2 :(得分:1)

路径= <强> * T *分机

这是一个错字问题。

答案 3 :(得分:0)

试试这个

<Label Content="{Binding Source={x:Reference txtName}, Path=text}" Height="28" HorizontalAlignment="Left" Margin="47,112,0,0" Name="label3" VerticalAlignment="Top" Width="171" />