2012-11-26 32 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> 

相關問題