2012-02-17 37 views
0

我有一个ScrollViewer中(NAME =“主”),一个StackPanel里面有一些示例边框或矩形,并且还具有更多的文本,比它的显示的一个文本框,使文本框的滚动。当您滚动文本框,来到顶部奥德底部边框的ScrollViewer中这是在文本框模板(ScrollViewer中X:NAME =“PART_ContentHost”)路由滚动到主的ScrollViewer外(“主”)。TextBlock中的ScrollViewer模板路由滚动?

是否可以编辑文本框模板以减弱此行为?

一种方式是与其他的ScrollViewer的模板组合,这工作得很好,但它#不是更可以看到选择所以这是没有办法了。

您有任何其他想法?

<Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}"> 
     <Setter Property="VerticalScrollBarVisibility" Value="Auto"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type TextBox}"> 
         <ScrollViewer x:Name="PART_ContentHost" /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 


<ScrollViewer Name="master" Height="71" Width="164" HorizontalAlignment="Right" Margin="0,0,38.666,28.833" VerticalAlignment="Bottom"> 
        <StackPanel Orientation="Vertical" Width="121"> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
         <TextBox Text="TextBox sydxr gs dgh drz h we rths dretghe dtrzuj hwesrtgh 
    bdnftzh srdztj ser tghbed5rsetzhnrd hserdfgcjmnjs egrfhfn dshgrdxthgj" TextWrapping="Wrap" Height="67" Style="{StaticResource detail_text}" /> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
         <Rectangle Fill="red" Height="45" Stroke="Black"/> 
        </StackPanel> 
      </ScrollViewer> 

非常感谢。

回答

0

我的建议是捕捉PreviewMouseWheelEvent上的文本框本身:

<TextBox Text="TextBox sydxr gs dgh drz h we rths dretghe dtrzuj hwesrtgh 
bdnftzh srdztj ser tghbed5rsetzhnrd hserdfgcjmnjs egrfhfn dshgrdxthgj" TextWrapping="Wrap" Height="67" ScrollViewer.VerticalScrollBarVisibility="Auto" PreviewMouseWheel="TextBox_PreviewMouseWheel" /> 

注意,我包括ScrollViewer.VerticalScrollBarVisibility="Auto"也。

然后评估,如果滚动正在取得时,无论是在顶部或底部,并标记事件作为处理如果是这样:

private void TextBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e) 
{ 
    var textbox = sender as TextBox; 
    var decorator = VisualTreeHelper.GetChild(textbox, 0) as Decorator; 
    var scrollViewer = decorator.Child as ScrollViewer; 

    if ((scrollViewer.VerticalOffset == scrollViewer.ScrollableHeight && e.Delta < 0) || 
     (scrollViewer.VerticalOffset == 0 && e.Delta > 0)) 
    { 
     e.Handled = true; 
    } 
} 

无需申请新的模板这样一来,干杯!