我正在使用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”属性的代码。
答案 0 :(得分: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]);
}
}
}
将逻辑更改为您想要的任何内容:)
答案 1 :(得分:0)
可能这个链接可以帮助...
http://kiwigis.blogspot.in/2010/12/how-to-add-scrollintoview-to.html
基本上,您需要查找ItemsControl
的项容器,例如ListBoxItem
,TreeViewItem
,ComboBoxItem
,ListViewItem
,{{1}等等,并调用其DataGridRow
函数。
在MVVM中,这可以通过附加行为来实现。
答案 2 :(得分: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>