Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ListBox项资源的备份和还原状态_C#_Wpf_Listbox - Fatal编程技术网

C# ListBox项资源的备份和还原状态

C# ListBox项资源的备份和还原状态,c#,wpf,listbox,C#,Wpf,Listbox,我想先取消填充,然后再重新填充一个ListBox,同时保持ListBox ItemsSource的先前状态(即当前水平和垂直滚动偏移以及选择) 以下是我目前的代码: ObservableCollection<string> items = new ObservableCollection<string>(); public MainWindow() { InitializeComponent(); for (int i = 1; i < 25;

我想先取消填充,然后再重新填充一个ListBox,同时保持ListBox ItemsSource的先前状态(即当前水平和垂直滚动偏移以及选择)

以下是我目前的代码:

ObservableCollection<string> items = new ObservableCollection<string>();

public MainWindow() {
    InitializeComponent();

    for (int i = 1; i < 25; i++) items.Add("");

    listBox.ItemsSource = items;
}

private void btnDepopulate_Click(object sender, RoutedEventArgs e) {
    //how to backup the state?
    listBox.ItemsSource = null;
}

private void btnRepopulate_Click(object sender, RoutedEventArgs e) {
    listBox.ItemsSource = items;
    //how to restore the state?
}
observetecollection items=新的observetecollection();
公共主窗口(){
初始化组件();
对于(inti=1;i<25;i++)项,添加(“”);
listBox.ItemsSource=项目;
}
私有无效BTNDEPLOCATE\U单击(对象发送者,路由目标e){
//如何备份状态?
listBox.ItemsSource=null;
}
私有void btnreplopulate\u单击(对象发送者,路由目标){
listBox.ItemsSource=项目;
//如何恢复状态?
}

使用此代码,当我重新填充列表框时,滚动属性和选择将被清除。

我强烈建议将列表框更改为Listview

由于ListView是从ListBox继承而来的,所以ListView中也包含ListBox的所有功能

适用于Windows 8.1及以上版本

我为什么要推荐?因为ListView具有帮助您保存和检索ListView相对滚动位置的功能

有关如何实现ListViewPersistenceHelper,请参见此

对于您的选择,您可以在App.xaml中创建一个全局变量

public int ListViewScrollSelectedIndex;
这将保存ListView的SelectedIndex。重新填充ListView时,可以将其设置为

ListView.SelectedIndex  = ListViewScrollSelectedIndex;
这应该兼顾两个问题

用于WPF

如果要获取列表框的当前滚动位置,需要首先访问其滚动查看器

下面是获取滚动查看器的代码

    private ScrollViewer GetObjectScrollViewer(DependencyObject dependencyObject)
    {
        if (dependencyObject is ScrollViewer)
            return dependencyObject as ScrollViewer;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            var _scrollViewer = GetObjectScrollViewer(VisualTreeHelper.GetChild(dependencyObject, i));
            if (_scrollViewer != null) return _scrollViewer;
        }
        return null;
    }
现在,当您再次填充它时,请指定scrollviewer的ScrollToVerticalOffset,以便它可以返回到相同的位置

var _ListBoxScrollViewer = GetObjectScrollViewer(ListBox);
if (_ListBoxScrollViewer != null) _ListBoxScrollViewer.ScrollToVerticalOffset(ScrollPosition);

希望这能有所帮助。

我强烈建议您将listbox更改为Listview

由于ListView是从ListBox继承而来的,所以ListView中也包含ListBox的所有功能

适用于Windows 8.1及以上版本

我为什么要推荐?因为ListView具有帮助您保存和检索ListView相对滚动位置的功能

有关如何实现ListViewPersistenceHelper,请参见此

对于您的选择,您可以在App.xaml中创建一个全局变量

public int ListViewScrollSelectedIndex;
这将保存ListView的SelectedIndex。重新填充ListView时,可以将其设置为

ListView.SelectedIndex  = ListViewScrollSelectedIndex;
这应该兼顾两个问题

用于WPF

如果要获取列表框的当前滚动位置,需要首先访问其滚动查看器

下面是获取滚动查看器的代码

    private ScrollViewer GetObjectScrollViewer(DependencyObject dependencyObject)
    {
        if (dependencyObject is ScrollViewer)
            return dependencyObject as ScrollViewer;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dependencyObject); i++)
        {
            var _scrollViewer = GetObjectScrollViewer(VisualTreeHelper.GetChild(dependencyObject, i));
            if (_scrollViewer != null) return _scrollViewer;
        }
        return null;
    }
现在,当您再次填充它时,请指定scrollviewer的ScrollToVerticalOffset,以便它可以返回到相同的位置

var _ListBoxScrollViewer = GetObjectScrollViewer(ListBox);
if (_ListBoxScrollViewer != null) _ListBoxScrollViewer.ScrollToVerticalOffset(ScrollPosition);

希望这有帮助。

我正在使用WPF,您的链接似乎是针对Windows应用程序(Windows 8和10)。我说得对吗?啊,我明白了。我正在编辑列表框本身的答案。@Drarig29有帮助吗?或者你需要更多关于这个问题的信息吗?我正在使用WPF,你的链接似乎是针对Windows应用程序(Windows 8和10)。我说得对吗?啊,我明白了。我正在编辑列表框本身的答案。@Drarig29有帮助吗?或者你需要更多关于这个问题的信息吗?