2017-01-17 47 views
0

如何將另一個用戶控件中的元素綁定到命令目標?如何使用wpf在另一個UserControl中移動滾動元素?

這是主要的XAML

<Grid> 
    <StackPanel> 
     <Button Height="200" x:Name="PageUpButton" FontFamily="Marlett" FontSize="40" Content="5" Command="{x:Static ScrollBar.PageUpCommand}" CommandTarget="{Binding ElementName=scrollViewerActive}"/> 
     <local:posMenuChild x:Name="PosMenuChild"/> 
     <Button Height="200" x:Name="PageDownButton" FontFamily="Marlett" FontSize="40" Content="6" Command="{x:Static ScrollBar.PageDownCommand}" CommandTarget="{Binding ElementName=ScrollViewerActive }"/> 
    </StackPanel>   
</Grid> 

我應該指定爲CommandTarget? 如何使用頂部窗口中的按鈕滾動下列UserControl中的元素?

這是用戶控件

<Grid Height="200"> 
    <WrapPanel Orientation="Vertical" Height="200"> 
     <ScrollViewer VerticalScrollBarVisibility="Hidden" Name="ScrollViewerActive" CanContentScroll="True" > 
      <StackPanel> 
       <TextBlock Text="Test1" FontSize="35"/> 
       <TextBlock Text="Test2" FontSize="35"/> 
       <TextBlock Text="Test3" FontSize="35"/> 
       <TextBlock Text="Test4" FontSize="35"/> 
       <TextBlock Text="Test5" FontSize="35"/> 
       <TextBlock Text="Test6" FontSize="35"/> 
      </StackPanel> 
     </ScrollViewer> 
    </WrapPanel> 
</Grid> 

回答

0

與以下修改您的用戶控件的代碼隱藏:

首先,添加一個DependencyProperty綁定到一個ScrollViewer類型的

public static readonly DependencyProperty ScrollTargetProperty = DependencyProperty.RegisterAttached(
     "ScrollTarget", typeof(ScrollViewer), typeof(UserControl1), new PropertyMetadata(null)); 

public static void SetScrollTarget(DependencyObject element, ScrollViewer value) 
{ 
    element.SetValue(ScrollTargetProperty, value); 
} 

public static ScrollViewer GetScrollTarget(DependencyObject element) 
{ 
    return (ScrollViewer)element.GetValue(ScrollTargetProperty); 
} 

不要忘記將UserControl1更改爲您的usercontrol的課程名稱。

然後,此屬性設置爲ScrollViewerActive(我做到了控件的構造函數中)

SetScrollTarget(this, ScrollViewerActive); 

現在你可以給它綁定這樣

<Button Command="{x:Static ScrollBar.PageUpCommand}" CommandTarget="{Binding Path=ScrollTarget, ElementName=PosMenuChild, Mode=OneWay}"/> 
相關問題