Why data is not bound to label in my WPF application

China☆狼群 提交于 2021-02-05 08:59:07

问题


I am doing simple DataBinding application in WPF. In this whatever I type in TextBox called txtName should get apeared on the Label. For this purpose i used binding in WPF.

But its not getting bound.

My 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>

I am new with WPF. Please guid me.


回答1:


Pretty sure XAML is case sensitive. Your binding path is text when it should be Text.




回答2:


TextBox has property Text, not text. Small mistake :)

 <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>



回答3:


Path=*T*ext

It's a typo problem.




回答4:


Try this

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


来源:https://stackoverflow.com/questions/16932906/why-data-is-not-bound-to-label-in-my-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!