【问题标题】:Binding TabControl ItemsSource to an ObservableCollection of ViewModels causes content to refresh on focus将 TabControl ItemsSource 绑定到 ViewModels 的 ObservableCollection 会导致内容在焦点上刷新
【发布时间】:2010-05-05 03:14:17
【问题描述】:

我正在使用 MVVM 框架创建一个 WPF 应用程序,并且我采用了 Josh Smith 关于 MVVM here... 的文章中的几个功能

最重要的是,我将 TabControl 绑定到 ViewModel 的 ObservableCollection。这意味着正在使用一个选项卡式 MDI 界面,该界面将 UserControl 显示为 TabItem 的内容。我在应用程序中看到的问题是,当我有多个选项卡并且在选项卡之间来回切换时,每次更改选项卡时都会引用内容。

如果您下载 Josh Smith 的源代码,您会发现他的应用也存在同样的问题。例如,单击“查看所有客户”按钮并向下滚动到 ListView 的底部。接下来单击“创建新客户”按钮。当您切换回 All Customer 视图时,您会注意到 ListView 滚动回顶部。如果您切换回 New Customer 选项卡并将光标放在其中一个 TextBoxes 中,然后切换到 All Customers 选项卡并返回,您会注意到光标现在消失了。

我想这是因为我使用的是 ObservableCollection,但我不能确定。有什么办法可以防止tab的内容在收到焦点时刷新?

编辑: 当我在我的应用程序上运行分析器时,我发现了我的问题。我正在为我的 ViewModels 定义一个 DataTemplate,因此它知道如何在 ViewModel 显示在选项卡中时呈现它......就像这样:

<DataTemplate DataType="{x:Type vm:CustomerViewModel}">
    <vw:CustomerView/>
</DataTemplate>

所以每当我切换到不同的选项卡时,它都必须重新创建 ViewModel。我通过将 ViewModels 的 ObservableCollection 更改为 UserControls 的 ObservableCollection 来临时修复它。但是,如果可能的话,我真的还是想使用 DataTemplates。有没有办法让 DataTemplate 工作?

【问题讨论】:

    标签: .net wpf mvvm tabcontrol observablecollection


    【解决方案1】:

    WPF 的默认行为是卸载不可见的项目,包括卸载不可见的 TabItem。这意味着当您返回选项卡时,TabItem 会重新加载,并且任何未绑定的内容(例如滚动位置)都会被重置。

    有一个很好的网站here,其中包含扩展 TabControl 并阻止它在切换选项卡时破坏其 TabItems 的代码,但它现在似乎不再存在。

    这是我用来防止该问题的代码。它最初来自该站点,尽管我对其进行了一些更改。它在切换选项卡时保留 TabItems 的ContentPresenter,并在您返回页面时使用它来重绘 TabItem。它占用了更多内存,但我发现它的性能更好,因为 TabItem 不再需要重新创建它上面的所有控件。

    // Extended TabControl which saves the displayed item so you don't get the performance hit of 
    // unloading and reloading the VisualTree when switching tabs
    
    // Obtained from http://eric.burke.name/dotnetmania/2009/04/26/22.09.28
    // and made a some modifications so it reuses a TabItem's ContentPresenter when doing drag/drop operations
    
    [TemplatePart(Name = "PART_ItemsHolder", Type = typeof(Panel))]
    public class TabControlEx : System.Windows.Controls.TabControl
    {
        // Holds all items, but only marks the current tab's item as visible
        private Panel _itemsHolder = null;
    
        // Temporaily holds deleted item in case this was a drag/drop operation
        private object _deletedObject = null;
    
        public TabControlEx()
            : base()
        {
            // this is necessary so that we get the initial databound selected item
            this.ItemContainerGenerator.StatusChanged += ItemContainerGenerator_StatusChanged;
        }
    
        /// <summary>
        /// if containers are done, generate the selected item
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
        {
            if (this.ItemContainerGenerator.Status == GeneratorStatus.ContainersGenerated)
            {
                this.ItemContainerGenerator.StatusChanged -= ItemContainerGenerator_StatusChanged;
                UpdateSelectedItem();
            }
        }
    
        /// <summary>
        /// get the ItemsHolder and generate any children
        /// </summary>
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            _itemsHolder = GetTemplateChild("PART_ItemsHolder") as Panel;
            UpdateSelectedItem();
        }
    
        /// <summary>
        /// when the items change we remove any generated panel children and add any new ones as necessary
        /// </summary>
        /// <param name="e"></param>
        protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
    
            if (_itemsHolder == null)
            {
                return;
            }
    
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Reset:
                    _itemsHolder.Children.Clear();
    
                    if (base.Items.Count > 0)
                    {
                        base.SelectedItem = base.Items[0];
                        UpdateSelectedItem();
                    }
    
                    break;
    
                case NotifyCollectionChangedAction.Add:
                case NotifyCollectionChangedAction.Remove:
    
                    // Search for recently deleted items caused by a Drag/Drop operation
                    if (e.NewItems != null && _deletedObject != null)
                    {
                        foreach (var item in e.NewItems)
                        {
                            if (_deletedObject == item)
                            {
                                // If the new item is the same as the recently deleted one (i.e. a drag/drop event)
                                // then cancel the deletion and reuse the ContentPresenter so it doesn't have to be 
                                // redrawn. We do need to link the presenter to the new item though (using the Tag)
                                ContentPresenter cp = FindChildContentPresenter(_deletedObject);
                                if (cp != null)
                                {
                                    int index = _itemsHolder.Children.IndexOf(cp);
    
                                    (_itemsHolder.Children[index] as ContentPresenter).Tag =
                                        (item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
                                }
                                _deletedObject = null;
                            }
                        }
                    }
    
                    if (e.OldItems != null)
                    {
                        foreach (var item in e.OldItems)
                        {
    
                            _deletedObject = item;
    
                            // We want to run this at a slightly later priority in case this
                            // is a drag/drop operation so that we can reuse the template
                            this.Dispatcher.BeginInvoke(DispatcherPriority.DataBind,
                                new Action(delegate()
                            {
                                if (_deletedObject != null)
                                {
                                    ContentPresenter cp = FindChildContentPresenter(_deletedObject);
                                    if (cp != null)
                                    {
                                        this._itemsHolder.Children.Remove(cp);
                                    }
                                }
                            }
                            ));
                        }
                    }
    
                    UpdateSelectedItem();
                    break;
    
                case NotifyCollectionChangedAction.Replace:
                    throw new NotImplementedException("Replace not implemented yet");
            }
        }
    
        /// <summary>
        /// update the visible child in the ItemsHolder
        /// </summary>
        /// <param name="e"></param>
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);
            UpdateSelectedItem();
        }
    
        /// <summary>
        /// generate a ContentPresenter for the selected item
        /// </summary>
        void UpdateSelectedItem()
        {
            if (_itemsHolder == null)
            {
                return;
            }
    
            // generate a ContentPresenter if necessary
            TabItem item = GetSelectedTabItem();
            if (item != null)
            {
                CreateChildContentPresenter(item);
            }
    
            // show the right child
            foreach (ContentPresenter child in _itemsHolder.Children)
            {
                child.Visibility = ((child.Tag as TabItem).IsSelected) ? Visibility.Visible : Visibility.Collapsed;
            }
        }
    
        /// <summary>
        /// create the child ContentPresenter for the given item (could be data or a TabItem)
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        ContentPresenter CreateChildContentPresenter(object item)
        {
            if (item == null)
            {
                return null;
            }
    
            ContentPresenter cp = FindChildContentPresenter(item);
    
            if (cp != null)
            {
                return cp;
            }
    
            // the actual child to be added.  cp.Tag is a reference to the TabItem
            cp = new ContentPresenter();
            cp.Content = (item is TabItem) ? (item as TabItem).Content : item;
            cp.ContentTemplate = this.SelectedContentTemplate;
            cp.ContentTemplateSelector = this.SelectedContentTemplateSelector;
            cp.ContentStringFormat = this.SelectedContentStringFormat;
            cp.Visibility = Visibility.Collapsed;
            cp.Tag = (item is TabItem) ? item : (this.ItemContainerGenerator.ContainerFromItem(item));
            _itemsHolder.Children.Add(cp);
            return cp;
        }
    
        /// <summary>
        /// Find the CP for the given object.  data could be a TabItem or a piece of data
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        ContentPresenter FindChildContentPresenter(object data)
        {
            if (data is TabItem)
            {
                data = (data as TabItem).Content;
            }
    
            if (data == null)
            {
                return null;
            }
    
            if (_itemsHolder == null)
            {
                return null;
            }
    
            foreach (ContentPresenter cp in _itemsHolder.Children)
            {
                if (cp.Content == data)
                {
                    return cp;
                }
            }
    
            return null;
        }
    
        /// <summary>
        /// copied from TabControl; wish it were protected in that class instead of private
        /// </summary>
        /// <returns></returns>
        protected TabItem GetSelectedTabItem()
        {
            object selectedItem = base.SelectedItem;
            if (selectedItem == null)
            {
                return null;
            }
    
            if (_deletedObject == selectedItem)
            { 
    
            }
    
            TabItem item = selectedItem as TabItem;
            if (item == null)
            {
                item = base.ItemContainerGenerator.ContainerFromIndex(base.SelectedIndex) as TabItem;
            }
            return item;
        }
    }
    

    我通常使用的 TabControl 模板如下所示:

    <Style x:Key="TabControlEx_NoHeadersStyle" TargetType="{x:Type local:TabControlEx}">
        <Setter Property="SnapsToDevicePixels" Value="true"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type localControls:TabControlEx}">
                    <DockPanel>
                        <!-- This is needed to draw TabControls with Bound items -->
                        <StackPanel IsItemsHost="True" Height="0" Width="0" />
                        <Grid x:Name="PART_ItemsHolder" />
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    【讨论】:

      【解决方案2】:

      我相信这与绑定模式有关 - 您是否尝试过 {Binding Mode=OneWay}? 绑定到任何“DataSource”对象也会导致同样的问题。

      我不确定单向绑定是否是您正在寻找的所需行为,但我会从那里开始。

      【讨论】:

      • 我试过了,但没用。我已经更新了我的问题...如果您可以提供任何意见,请告诉我。谢谢。
      【解决方案3】:

      它与 ObservableCollection 无关。这是因为客户的视图被重用,而不是 WPF 为每个客户创建一个新的视图。

      您可以查看 WPF Application Framework (WAF) 的 Writer 示例应用程序。它还为选项卡式 MDI 界面实现了 TabControl,但不会遇到您在帖子中提到的问题。这个问题在 Writer 中通过为每个文档“选项卡”创建一个自己的 UserControl 来解决。

      【讨论】:

      • 谢谢@jbe,我知道我可以使用 UserControls 的 ObservableCollection 来解决问题,但如果可能的话,我更喜欢使用 ViewModels 的 ObservableCollection。我已经更新了我的问题...如果您可以提供任何意见,请告诉我。谢谢。
      • 当我开始使用 MVVM 模式时,我也使用了 WPF 的 DataTemplate 机制来将 View 与 ViewModel 连接在一起。但是我遇到了一些问题,例如您在此处提到的问题。这就是我将它们与 MEF 连接在一起的原因。 WPF 只是获取视图(用户控件),因此我不再需要此场景的 DataTemplate。
      猜你喜欢
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 2016-11-01
      • 1970-01-01
      相关资源
      最近更新 更多