简体   繁体   English

WPF的新增功能-在导致NullReferenceException的背后更新代码中的TextBlock

[英]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. 我真的是WPF的新手,并且只要列表框中的选定项发生更改,我就会尝试更新TextBlock中的文本。

I added the ListBox and TextBlock to my XAML: 我将ListBox和TextBlock添加到了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. 但是,当我运行此命令时,出现NullReferenceException错误。 I think I need to initialize the TextBlock somehow, but I'm not sure how to do this. 我认为我需要以某种方式初始化TextBlock,但是我不确定如何执行此操作。

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: 如果那不能满足您的需求,我将在您尝试访问它之前检查一下null:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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