简体   繁体   中英

How to Update Text of TextBlock in WPF

Whenever I am Trying to update my Text to TextBlock. It is throwing the following exception: "Object reference set to null".

My Code:

<UniformGrid Grid.Row="1" Grid.Column="1" Height="30" >
    <Border DockPanel.Dock="Top">
        <TextBlock Foreground="White" TextAlignment="Justify" VerticalAlignment="Center" x:Name="TbcontentName" FontWeight="SemiBold"  />
    </Border>
</UniformGrid>

i am using this TextBlock In another Window

if((setMainWindowCall.Try("TbcontentName") as TextBox).Text != null)
    (setMainWindowCall.FindName("TbcontentName") as TextBox).Text = _nameOfUc;

You are using ... as TextBox where you define TbcontentName to be a TextBlock in your XAML, so the dynamic cast returns null and a NullPointerException is thrown when you try to access the .Text -Property. Either change as TextBox to as TextBlock in your code or change

<TextBlock ... x:Name="TbcontentName" ... />

to

<TextBox ... x:Name="TbcontentName" ... />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM