【问题标题】:WPF Programmatically Moving the Selected Item of a ListView into ViewWPF 以编程方式将 ListView 的选定项移动到视图中
【发布时间】:2018-07-24 00:24:51
【问题描述】:

我在 ListView1 中使用了一个 SelectionChanged 事件,它更改了一个字符串:“dr.CustomBackgroundColor”。该字符串用于查找索引并更改目标 ListView 中的选择:“SetColorListView”。哪个按预期工作!

但“SetColorListView”中的选择不会“跳转”到视图中。 IE。它被选中了,但我必须滚动才能找到它...

有没有一种简单的方法可以让 SelectedItem 自动进入视图?

<ListView x:Name="SetColorListView" Focusable="False"
     ItemsSource="{Binding SystemColorObservableCollection, UpdateSourceTrigger=PropertyChanged}"
     SelectedValuePath="HexValue" 
     SelectedValue="{Binding objGantChartClass.CustomBackgroundColor, Mode=TwoWay}" 
     SelectedIndex="{Binding SystemColorObservableCollectionSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsSynchronizedWithCurrentItem="True">

SystemColorObservableCollectionSelectedIndex = SystemColorObservableCollection.IndexOf(SystemColorObservableCollection.Where(c => c.HexValue == dr.CustomBackgroundColor).FirstOrDefault());

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    您可以使用行为:

    using System.Windows.Controls;
    using System.Windows.Interactivity;
    
    namespace Jarloo.Sojurn.Behaviors
    {
        public sealed class ScrollIntoViewBehavior : Behavior<ListBox>
        {
            protected override void OnAttached()
            {
                base.OnAttached();
                AssociatedObject.SelectionChanged += ScrollIntoView;
            }
    
            protected override void OnDetaching()
            {
                AssociatedObject.SelectionChanged -= ScrollIntoView;
                base.OnDetaching();
            }
    
            private void ScrollIntoView(object o, SelectionChangedEventArgs e)
            {
                var b = (ListBox)o;
                if (b == null) return;
                if (b.SelectedItem == null) return;
    
                var item = (ListBoxItem)((ListBox)o).ItemContainerGenerator.ContainerFromItem(((ListBox)o).SelectedItem);
                if (item != null) item.BringIntoView();
            }
        }
    }
    

    然后在你的 ListBox 中声明它:

    <ListBox ItemsSource="{Binding Shows.View}" Background="{x:Null}"  Grid.Row="1" Grid.Column="0"
                ScrollViewer.HorizontalScrollBarVisibility="Disabled" Style="{StaticResource NoFocusListBoxStyle}" SelectedItem="{Binding SelectedShow}">
    
        <i:Interaction.Behaviors>
            <behaviors:ScrollIntoViewBehavior />
        </i:Interaction.Behaviors>
    
        <ListBox.ItemTemplate>
            <DataTemplate>
    

    ...

    您还需要以下声明为命名空间:

    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    

    我从我在 GitHub 上的一个开源项目中获取了这段代码,如果您需要查看它的实际运行情况或想要更多上下文,可以在这里查看完整源代码 Sojurn

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多