简体   繁体   中英

New to WPF - Updating TextBlock in Code behind causing NullReferenceException

I'm really new to WPF and I'm trying to update the text in a TextBlock whenever the selected item in a ListBox changes.

I added the ListBox and TextBlock to my XAML:

<Window x:Class="Blend_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" WindowState="Maximized" ResizeMode="NoResize" Width="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidthKey}}" Height="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeightKey}}">
<Grid Background="#FFC10000">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <ListBox Grid.Column="0" Margin="20" FontSize="48" Name="VideoListBox" SelectedIndex="0" Cursor="None" SelectionChanged="VideoListBox_SelectionChanged">
        <ListBoxItem Margin="20">Video 1</ListBoxItem>
        <ListBoxItem Margin="20">Video 2</ListBoxItem>
        <ListBoxItem Margin="20">Video 3</ListBoxItem>
        <ListBoxItem Margin="20">Video 4</ListBoxItem>
    </ListBox>
    <TextBlock Grid.Column="1" Text="Lorem Ipsum" x:Name="VideoTextBlock" FontSize="48"></TextBlock>        
</Grid>
</Window>

But now I'm not exactly sure what to add to my code behind. What I have so far is:

    public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void VideoListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        VideoTextBlock.Text = "Test";
    }
}

However when I run this I'm getting a NullReferenceException error. I think I need to initialize the TextBlock somehow, but I'm not sure how to do this.

Try using a binding rather than an event handler:

<Window
    x:Class="Blend_Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    WindowState="Maximized"
    ResizeMode="NoResize"
    Width="{DynamicResource {x:Static SystemParameters.PrimaryScreenWidthKey}}"
    Height="{DynamicResource {x:Static SystemParameters.PrimaryScreenHeightKey}}">
    <Grid Background="#FFC10000">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <ListBox
            Grid.Column="0"
            Margin="20"
            FontSize="48"
            Name="VideoListBox"
            SelectedIndex="0"
            Cursor="None">
            <ListBoxItem Margin="20">Video 1</ListBoxItem>
            <ListBoxItem Margin="20">Video 2</ListBoxItem>
            <ListBoxItem Margin="20">Video 3</ListBoxItem>
            <ListBoxItem Margin="20">Video 4</ListBoxItem>
        </ListBox>
        <TextBlock
            Grid.Column="1"
            Text="{Binding SelectedItem.Content, ElementName=VideoListBox}"
            x:Name="VideoTextBlock"
            FontSize="48"/>
    </Grid>
</Window>

If that doesn't work for your needs, I would just check for null before you try to access it:

private void VideoListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if (VideoTextBlock != null)
    {
        VideoTextBlock.Text = "Test";
    }
}

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