WPF ContentPresenter不适用于ResourceDictionary中的单个条目

时间:2018-09-04 08:23:50

标签: c# wpf mvvm resourcedictionary contentpresenter

我的ContentPresenter有一个奇怪的问题。在我的标准WPF应用程序中,我有一个“ TestView”,一个“ TestViewModel”,一个“ MainWindowView”和一个“ MainWindowViewModel”。 MainWindowView包含ContentPresenter,该内容绑定到MainWindowViewModel的ViewModelProperty:

<ContentPresenter DockPanel.Dock="Right"
                  Height="Auto"
                  Width="Auto"
                  Content="{Binding CurrentContentViewModel}" />

和ViewModel:

private IViewModel _currentContentViewModel;
public IViewModel CurrentContentViewModel
{
    get
    {
        return  _currentContentViewModel;
    }
    set
    {
        _currentContentViewModel = value;
        OnPropertyChanged();
    }
 }

TestView绑定到我的App.xaml中的T​​estViewModel:

<Application.Resources>
    <ResourceDictionary >
        <DataTemplate DataType="{x:Type viewModel:TestViewModel}">
            <view:TestView />
        </DataTemplate>
    </ResourceDictionary>
</Application.Resources>

现在我将TestViewModel的实例分配给CurrentContentViewModel时,ContentPresenter将显示TestViewModel的类名称:

MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
MainWindowView mainWindowView = new MainWindowView(mainWindowViewModel);

TestViewModel testViewModel = new TestViewModel();

mainWindowView.Show();
mainWindowViewModel.CurrentContentViewModel = testViewModel;

我如何解决该问题?我在ResourceDictionary中添加了一个空样式标签,一个SolidColorBrush定义或其他内容:

<Application.Resources>
    <ResourceDictionary >
        <DataTemplate DataType="{x:Type viewModel:TestViewModel}">
            <view:TestView />
        </DataTemplate>

        <SolidColorBrush x:Key="ColorBrushDummy" Color="HotPink"/>

    </ResourceDictionary>
 </Application.Resources>

现在一切正常,ContentPresenter会显示TestView。

现在我的问题是:为什么?

编辑:

我制作了一个新的SampleProject并重现了该错误。当我开始使用App.xaml.cs的OnStartup方法时,会出现问题:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
    MainWindowView mainWindowView = new MainWindowView(mainWindowViewModel);

    TestViewModel testViewModel = new TestViewModel();

    mainWindowView.Show();
    mainWindowViewModel.CurrentContentViewModel = testViewModel;
}

我的TestView:

<UserControl x:Class="ErrorSample.TestView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:ErrorSample"
         mc:Ignorable="d" 
         d:DesignHeight="450"
         d:DesignWidth="800"
         d:DataContext="{d:DesignInstance local:TestViewModel}">
<Grid Background="Red">
        <TextBlock Text="{Binding Text}"
                   VerticalAlignment="Center"
                   HorizontalAlignment="Center"/>
</Grid>

我的TestViewModel:

public class TestViewModel : ViewModelBase
{
    public string Text { get; } = "Sample Text";
}

还有我的MainWindowViewModel:

public class MainWindowViewModel : ViewModelBase
{
    private IViewModel _currentContentViewModel;

    public IViewModel CurrentContentViewModel
    {
        get => _currentContentViewModel;
        set
        {
            _currentContentViewModel = value;
            OnPropertyChanged();
        }
    }

    public MainWindowViewModel()
    {

    }
}

0 个答案:

没有答案