切换到相同DataType的对象后,DataTemplate不刷新

编程入门 行业动态 更新时间:2024-10-21 03:40:08
本文介绍了切换到相同DataType的对象后,DataTemplate不刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下 ControlTemplate :

<ContentControl Content="{Binding ContentViewModel}"> <ContentControl.Resources> <DataTemplate DataType="{x:Type viewModel:FilledContentViewModel}"> <usercontrols:FilledMainWindow x:Name="MainContent" /> </DataTemplate> <DataTemplate DataType="{x:Type viewModel:EmptyContentViewModel}"> <!-- todo: Something like a StartPage here --> </DataTemplate> </ContentControl.Resources> </ContentControl>

这很好用,直到视图模型尝试将 ContentViewModel 属性从一个 FilledContentViewModel 更改为新的 FilledContentViewModel ,然后将其内容 ContentControl 不刷新.如果从 EmptyContentViewModel 切换到 FilledContentViewModel 或以其他方式进行切换,则可以正常工作.

This works great until the view model tries to change the ContentViewModel property from one FilledContentViewModel to a new FilledContentViewModel, then the content of the ContentControl does not refresh. If switching from EmptyContentViewModel to FilledContentViewModel or the other way around, it works.

当然,仅更新现有 FilledContentViewModel 中的所有内容都是一种解决方案.但是我认为这可能很快就会变得混乱,仅创建具有正确上下文的新 ViewModel 并进行切换会更加优雅.

Of course, just updating everything in the existing FilledContentViewModel instead would be one solution. But I think it could get messy quick and that just creating a new ViewModel with the right context and switch it would be more elegant.

有人知道一种让 DataTemplate 的内容刷新的方法吗?

Does anybody know a way to let the content of the DataTemplate refresh?

推荐答案

我遇到了与 ContentPresenter 控件类似的问题,在该控件中,我会将 Content 切换为具有相同视图模型类型的新实例,并且没有刷新.我发现在social.msdn.microsoft上遇到了几乎相同的问题,最终使用的解决方案是创建自定义的 ContentControl / ContentPresenter 作为social.msdn.microsoft答案的一部分:

I ran into a similar problem with the ContentPresenter control where I would switch the Content to be a new instance of the same view model type and it wasn't refreshing. I found nearly the identical problem I was running into on social.msdn.microsoft and the solution I ended up using was to create a custom ContentControl / ContentPresenter that was posted as part of the social.msdn.microsoft answer:

social.msdn.microsoft/Forums/vstudio/zh-CN/6bc0a0ed-cb98-4863-a09e-5c99d0ddf90e/mvvm-contentcontrols-view-will-not-refresh?forum = wpf

public class MyContentControl : ContentControl { public MyContentControl() { this.ContentTemplateSelector = new MyDataTemplateSelector(); } class MyDataTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { var declaredDataTemplate = FindDeclaredDataTemplate(item, container); var wrappedDataTemplate = WrapDataTemplate(declaredDataTemplate); return wrappedDataTemplate; } private static DataTemplate WrapDataTemplate(DataTemplate declaredDataTemplate) { var frameworkElementFactory = new FrameworkElementFactory(typeof(ContentPresenter)); frameworkElementFactory.SetValue(ContentPresenter.ContentTemplateProperty, declaredDataTemplate); var dataTemplate = new DataTemplate(); dataTemplate.VisualTree = frameworkElementFactory; return dataTemplate; } private static DataTemplate FindDeclaredDataTemplate(object item, DependencyObject container) { var dataTemplateKey = new DataTemplateKey(item.GetType()); var dataTemplate = ((FrameworkElement)container).FindResource(dataTemplateKey) as DataTemplate; if (dataTemplate == null) throw new Exception("datatemplate not found"); return dataTemplate; } } }

更多推荐

切换到相同DataType的对象后,DataTemplate不刷新

本文发布于:2023-11-07 05:34:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:切换到   对象   DataType   DataTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!