使WPF TextBlock不可点击

时间:2017-12-08 13:02:37

标签: c# wpf xaml

我有一个简单的UserControl,带有TextBlock和两个按钮。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="150" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Height="30" Text="Please install updates" Foreground="#FFF" FontWeight="Bold" Margin="0,0,0,20" Grid.RowSpan="2"/>
    <Button Grid.Column="0" Grid.Row="1" Content="Restart Now" Margin="5 0 0 0" Width="80" Height="25" HorizontalAlignment="Right" />
    <Button Grid.Column="1" Grid.Row="1" Content="Snooze" Width="60" Height="25" Margin="10 0 0 0" HorizontalAlignment="Left" Click="bttnOK_Click" />
</Grid>

由于某些原因,TextBlock是可点击的,当您单击它时,它会关闭控件。 enter image description here

如何使TextBlock无法点击?

1 个答案:

答案 0 :(得分:2)

您似乎添加了<TextBlock ... Grid.RowSpan=2/>,这使TextBlock进入按钮区域。 或者,如果在TextBlock中删除RowSpan并不能解决问题,请尝试将IsHitTestVisible=false添加到TextBlock。

看起来像这样,

<Grid>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="150" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition Height="*" />
    <RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" Grid.Row="0" Grid.ColumnSpan="2" Height="30" Text="Please install updates" Foreground="#FFF" FontWeight="Bold" Margin="0,0,0,20"/>
<Button Grid.Column="0" Grid.Row="1" Content="Restart Now" Margin="5 0 0 0" Width="80" Height="25" HorizontalAlignment="Right" />
<Button Grid.Column="1" Grid.Row="1" Content="Snooze" Width="60" Height="25" Margin="10 0 0 0" HorizontalAlignment="Left" Click="bttnOK_Click" />