2

我对 WPF 很陌生,每当 ListBox 中的选定项目发生更改时,我都会尝试更新 TextBlock 中的文本。

我将 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>

但是现在我不确定要在我的代码后面添加什么。到目前为止,我所拥有的是:

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

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

但是,当我运行它时,我收到了 NullReferenceException 错误。我想我需要以某种方式初始化 TextBlock,但我不知道该怎么做。

4

1 回答 1

4

尝试使用绑定而不是事件处理程序:

<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>

如果这不能满足您的需求,我会在您尝试访问它之前检查 null:

private void VideoListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
    if (VideoTextBlock != null)
    {
        VideoTextBlock.Text = "Test";
    }
}
于 2013-07-30T23:33:40.663 回答