WPF 新手 - 在代码中更新 TextBlock 导致 NullReferenceException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17959096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:19:22  来源:igfitidea点击:

New to WPF - Updating TextBlock in Code behind causing NullReferenceException

c#wpfxaml

提问by MattSavage

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 的新手,每当 ListBox 中的选定项目发生更改时,我都会尝试更新 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. I think I need to initialize the TextBlock somehow, but I'm not sure how to do this.

但是,当我运行它时,我收到了 NullReferenceException 错误。我想我需要以某种方式初始化 TextBlock,但我不确定如何做到这一点。

回答by Dan

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