DataTemplate does not refresh after switching to an object of same DataType

青春壹個敷衍的年華 提交于 2021-02-07 09:10:47

问题


I have following 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>

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.

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.

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


回答1:


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.com and the solution I ended up using was to create a custom ContentControl / ContentPresenter that was posted as part of the social.msdn.microsoft.com answer:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/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;
        }
    }
}


来源:https://stackoverflow.com/questions/34557630/datatemplate-does-not-refresh-after-switching-to-an-object-of-same-datatype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!