【问题标题】:Make WPF TextBlock non clickable [closed]使WPF TextBlock不可点击[关闭]
【发布时间】:2017-12-08 13:02:37
【问题描述】:

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

<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 是可点击的,当您点击它时,它会关闭控件。

期望的行为:不可点击的文本块。单击 TextBlock 时,控件保持打开状态。

【问题讨论】:

  • 通常它们是不可点击的。也许您有一种覆盖默认属性的样式?将IsCancel 设置为true 的位置。
  • 你能提供更多关于父对象/样式的细节吗?
  • 我刚刚将您的 XAML 复制到了一个全新的项目中,Textblock不可点击的。问题一定出在其他地方。
  • 这个UserControl在telerik DesktopAlert里面,也许这就是导致问题的原因。

标签: c# wpf xaml


【解决方案1】:

您似乎添加了&lt;TextBlock ... Grid.RowSpan=2/&gt;,这使得TextBlock进入了Buttons区域。 或者,如果删除 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" />

【讨论】:

  • 即使 RowSpan=2 按钮仍然在 TextBlock 的顶部。那有什么意义呢?
  • 视觉上它们是......,但可能是按钮边框可能已经扩展到 TextBlock。或者可能是您包含此 UserControl 的容器是可单击的。尝试将 IsHitTestVisible=false 添加到容器中。
  • IsHitTestVisible="False" 有帮助。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
相关资源
最近更新 更多