2
votes

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.

1
Does it happen on VideoTextBlock.Text = "Test";? - PoweredByOrange
Yes, that's when the error occurs. - MattSavage
That's because the ListBox is being created before the TextBlock in your XAML, and once a ListBox is created, the SelectionChanged event is triggered - notice that at this point the TextBlock is still NOT created, so you get a NulReferenceException. Not sure what your design is, but you can check to see if the TextBlock is null or not before changing its text. Once the window is shown, you'll notice that changing the selected item will actually update the TextBlock. - PoweredByOrange

1 Answers

4
votes

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";
    }
}