如何使Panel响应Tap事件但不允许编辑TextBox?

时间:2011-12-04 21:26:51

标签: silverlight windows-phone-7.1 windows-phone-7

我有以下代码:

public class IdentityCell : Panel
{
  public IdentityCell()
  {
    Margin = m_zeroThickness;
    VerticalAlignment = VerticalAlignment.Top;
    Width = double.NaN;

    m_labelIcon = new Image();
    {
      m_labelIcon.VerticalAlignment = VerticalAlignment.Center;
      m_labelIcon.Width = getIconSizeToUseInPixels();
      m_labelIcon.Height = getIconSizeToUseInPixels();
    }
    Children.Add(m_labelIcon);

    m_labelName = new TextBox();
    {
      m_labelName.Margin = m_zeroThickness;
      m_labelName.VerticalAlignment = VerticalAlignment.Center;
      m_labelName.TextAlignment = TextAlignment.Center;

      m_labelName.FontSize = (double)
        Application.Current.Resources["PhoneFontSizeMediumLarge"];

      m_labelName.Padding = m_zeroThickness;
      m_labelName.IsHitTestVisible = false;
      m_labelName.BorderThickness = m_zeroThickness;
      m_labelName.Width = double.NaN;
      m_labelName.Foreground = m_phoneForegroundBrush;
      m_labelName.Background = null;
    }
    Children.Add(m_labelName);

    IsHitTestVisible = true;
    Tap += onTap;
  }

  ...
}

点击m_labelIcon时,会调用onTap()。但是当点击m_labelName时,不会调用onTap()。如果我将m_labelName.IsHitTestVisible设置为true,则点击m_labelName会使控件进入编辑模式并弹出屏幕键盘。 m_labelName应该只是一个静态文本控件而没有编辑支持。我尝试过使用IsEnabled或IsReadOnly属性,但这会改变外观。

我的TextBox位于我的图片旁边。布局如下所示:

---- Panel --------------------
<Image> <       TextBox       >
---- Panel --------------------

任何帮助将不胜感激!

由于

1 个答案:

答案 0 :(得分:4)

如果 m_labelName 应该只是一个没有编辑支持的静态文本控件,请尝试使用 TextBlock

在msdn上查看TextBlock Class