0

我正在使用 MVVM。我在属性 InputTemplates 中有一个 xml 节点列表。当我从视图模型中选择列表的任何项目时,我希望列表自动向下滚动。我知道我必须在我的视图模型中使用一个属性“SelectedItem”,这会有所帮助。

<Border BorderBrush="{StaticResource BorderBrush}" 
IsEnabled="{Binding Path=InputTemplateBorderEnabled}" 
BorderThickness="2" CornerRadius="5" Canvas.Left="1" Canvas.Top="84" Height="240" Name="border7" Width="432" >
      <HeaderedContentControl    
                Content="{Binding Path=InputTemplates,Mode=OneTime}"
                Header="{Binding Path=INTemplateLabel}"
                ContentTemplate="{StaticResource FileTabTemplate}"
                Style="{StaticResource MainHCCStyle}" Width="420" Height="237" />
</Border>
 <DataTemplate x:Key="FileTabTemplate">
        <st:ScrollableTabControl Background="#FFF0F9F8"  
          IsSynchronizedWithCurrentItem="True" 
          ItemsSource="{Binding}" 
          ItemTemplate="{StaticResource FileTabItemTemplate}"                 
          Margin="1">
        </st:ScrollableTabControl>
</DataTemplate>
<DataTemplate x:Key="FileTabItemTemplate" >       
        <DockPanel>
            <TextBlock Name="textBlock" Text="{Binding Path=Keyword}" ToolTip="{Binding Path=FileName}"  FontFamily="Microsoft Sans Serif" FontSize="10"  TextWrapping="NoWrap"/>
        </DockPanel>

        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                <Setter TargetName="textBlock" Property="Foreground" Value="Indigo"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>
ObservableCollection<CommonResource.ViewModel.FileTemplateViewModel> inputTemplates;

foreach (XMLTemplateViewModel xmlvm in inputTemplates)
{
   list = xmlvm.XMlRootNodes[iSearchRootNode];
   list.SelectedItem = MyList[iSelectionIndex];// MyList is a list of few items TreeViewWithIcons
}

在设置 list.SelectedItem 上,自动向下滚动后,所选项目应在屏幕中可见。我必须为此使用任何事件吗?还请提供属性“SelectedItem”的代码。

4

3 回答 3

1

当您使用 ListBox 或 ListView 时,您可以尝试以下操作:

public class ScrollIntoViewBehavior:Behavior<ListBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.SelectionChanged += new SelectionChangedEventHandler(AssociatedObject_SelectionChanged);
    }

    void AssociatedObject_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            (sender as ListBox).ScrollIntoView(e.AddedItems[0]);
        }
    }
}

将逻辑更改为您想要的任何内容:)

于 2012-12-14T08:10:23.557 回答
0

这是 XML 项目视图中的数据格式。我想知道如何添加触发器,以便在选择 HeaderTextBlock 时,滚动条向下移动以使所选项目可见。

                <HierarchicalDataTemplate  DataType="{x:Type cr:TreeViewWithIcons}" ItemsSource="{Binding Path=ChildNodes,Mode=OneTime}">                       
                        <StackPanel Orientation="Horizontal" Name="stackpanel" IsEnabled="False" >
                        <Image Source="{Binding Path=Icon}"/>
                        <TextBlock Name="HeaderTextBlock" Text="{Binding Path=HeaderText}" Background="{Binding Path=BackgroundColor,Mode=TwoWay,NotifyOnSourceUpdated=True}" Foreground="{Binding Path=HeaderColor,Mode=TwoWay,NotifyOnSourceUpdated=True}" IsEnabled="False">
                        </TextBlock>
                    </StackPanel>
                    <HierarchicalDataTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource= {RelativeSource Mode=FindAncestor, AncestorType= {x:Type TreeViewItem}},Path=IsSelected}" Value="True">
                            <Setter TargetName="HeaderTextBlock" Property="Foreground" Value="Red"/>
                            <Setter TargetName="stackpanel" Property="Background" Value="LightGray"/>
                            <Setter TargetName="HeaderTextBlock" Property="Background" Value="LightGray"/>
                            <Setter Property="BitmapEffect">
                                <Setter.Value>
                                    <OuterGlowBitmapEffect GlowColor="Black" />
                                </Setter.Value>
                            </Setter>
                        </DataTrigger>
                        <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource TemplatedParent}}" Value="True">
                            <Setter TargetName="HeaderTextBlock" Property="Foreground" Value="Purple"/>
                            <Setter TargetName="HeaderTextBlock" Property="Visibility" Value="Visible"/>
                        </DataTrigger>
                    </HierarchicalDataTemplate.Triggers>
                </HierarchicalDataTemplate>
            </ResourceDictionary>
        </cr:ExtendedTreeView.Resources>
于 2012-12-14T09:41:35.160 回答
0

可能这个链接可以帮助......

http://kiwigis.blogspot.in/2010/12/how-to-add-scrollintoview-to.html

基本上你需要找出你的物品容器,例如ItemsControl,,,ListBoxItem等等,TreeViewItem并调用它的函数。ComboBoxItemListViewItemDataGridRowBringIntoView()

在 MVVM 中,这可以通过附加行为来实现。

于 2012-12-14T05:48:09.343 回答