2012-11-26 29 views
1

这是一个设置:我有一个数字值的文本框。根据要求,每次有人需要提供相应的评论时,都需要进行更改。因此,视觉上必须有另一个文本框用于评论,该文本框应该显示在第一个旁边。理想情况下,注释文本需要被放置在距离值文本框发起标注,并显示在右侧从它覆盖的任何事情是它下面就像在这张照片:enter image description here如何将一个控件放在从XAML中的另一个控件开始的标注中?

我知道该怎么做很容易在CSS和HTML。 我现在必须在Silverlight中做同样的事情。 可惜我不是很强烈,所以什么我特别问如何有2个文本框,让其中一人旁边出现另一个(上覆盖任何控制是它下面的右)为少XAML和代码可能。

回答

0

使用ToolTip,并设置布局,使得它出现在右边。在XAML中,您可以模板您ToolTip看不过你想要的,即使这意味着模仿TextBox外观。

这是ToolTip的目的,我强烈地感受到你应该总是使用正确的工具合适的工作。 :)

我希望这有助于。让我们知道您是否需要代码示例。

编辑:添加以下代码示例:

<TextBox ToolTipService.Placement="Right"> 
     <ToolTipService.ToolTip> 
      <TextBox Text="{Binding CalloutText, Mode=OneWay}" IsReadOnly="True"/> 
     </ToolTipService.ToolTip> 
    </TextBox> 
+0

我确实需要样品。 –

+0

如何控制其位置? –

+0

您可以使用'ToolTipService.Placement'' AttachedProperty'设置位置。您还可以使用“ToolTipService.VerticalOffset”和“ToolTipService.Horizo​​ntalOffset”来调整定位。如果你需要更多的控制,可以考虑使用'ToolTipService.PlacementRectangle'。 – XamlZealot

0

好吧,我结束了写我自己的行为

namespace MyNamespace 
{ 
    public class CommentBehavior : Behavior<TextBox> 
    { 
     private readonly TimeSpan howLongWeWaitBeforePopupCloses = TimeSpan.FromMilliseconds(200); 

     private DispatcherTimer popupClosingTimer; 
     public static DependencyProperty PopupProperty = DependencyProperty.Register("Popup", typeof(Popup), typeof(CommentBehavior), new PropertyMetadata(null)); 

     public Popup Popup 
     { 
      get { return (Popup)this.GetValue(PopupProperty); } 
      set { this.SetValue(PopupProperty, value); } 
     } 

     protected override void OnAttached() 
     { 
      this.popupClosingTimer = new DispatcherTimer(); 
      this.popupClosingTimer.Stop(); 
      this.popupClosingTimer.Interval = howLongWeWaitBeforePopupCloses; 
      this.popupClosingTimer.Tick += this.ClosePopup; 
      this.AssociatedObject.GotFocus += this.GotFocus; 
      this.AssociatedObject.LostFocus += this.LostFocus; 
      this.Popup.Child.GotFocus += PopupChild_GotFocus; 
      this.Popup.Child.LostFocus += PopupChild_LostFocus; 
     } 

     private void PopupChild_LostFocus(object sender, RoutedEventArgs e) 
     { 
      this.popupClosingTimer.Start(); 
     } 

     private void PopupChild_GotFocus(object sender, RoutedEventArgs e) 
     { 
      this.popupClosingTimer.Stop(); 
     } 

     protected override void OnDetaching() 
     { 
      this.AssociatedObject.GotFocus -= this.GotFocus; 
      this.AssociatedObject.LostFocus -= this.LostFocus; 
      this.Popup.GotFocus -= PopupChild_GotFocus; 
      this.popupClosingTimer.Tick -= this.ClosePopup; 
      this.popupClosingTimer = null; 
     } 

     private void ClosePopup(object sender, EventArgs e) 
     { 
      this.Popup.IsOpen = false; 
      this.popupClosingTimer.Stop(); 
     } 

     protected void GotFocus(object sender, RoutedEventArgs e) 
     { 
      this.popupClosingTimer.Stop(); 
      this.Popup.IsOpen = true; 
      var at = this.CalculatePopupPosition(); 
      this.Popup.HorizontalOffset = at.X; 
      this.Popup.VerticalOffset = at.Y; 
     } 

     private Point CalculatePopupPosition() 
     { 
      var owner = this.AssociatedObject; 
      var transformation = owner.TransformToVisual(Application.Current.RootVisual); 
      var at = transformation.Transform(new Point(owner.ActualWidth, 0)); 
      return at; 
     } 

     protected void LostFocus(object sender, RoutedEventArgs e) 
     { 
      this.popupClosingTimer.Start(); 
     } 
    } 
} 

而下面的XAML

<Grid x:Name="LayoutRoot" Background="White"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto" /> 
    </Grid.RowDefinitions> 

    <StackPanel Grid.Row="0" Background="Red"> 
     <TextBox Width="200" Text="0.01"> 
      <i:Interaction.Behaviors> 
       <local:CommentBehavior> 
        <local:CommentBehavior.Popup> 
         <Popup> 
          <TextBox Text="Comment" /> 
         </Popup> 
        </local:CommentBehavior.Popup> 
       </local:CommentBehavior> 
      </i:Interaction.Behaviors> 
     </TextBox> 
    </StackPanel> 

</Grid> 

相关问题