c# - 用户控件内容未呈现

标签 c# wpf mvvm

我在我的 WPF 应用程序中跟踪了代码,但是当我更改下拉列表中的值时没有看到任何用户控件内容。您能告诉我我错过了什么吗?

MainWindow.xaml:

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:MainViewModel="clr-namespace:Test.ViewModel"
        xmlns:ViewModel="clr-namespace:Test.ViewModel.AccountTypes"
        xmlns:View="clr-namespace:Test.View" x:Class="Test.MainWindow"
        xmlns:Views="clr-namespace:Test.View.AccountTypes"
        xmlns:v="clr-namespace:Test.View.AccountTypes"
        xmlns:vm="clr-namespace:Test.ViewModel.AccountTypes"
        Title="{Binding DisplayName, Mode=OneWay}" ResizeMode="CanResize" WindowStartupLocation="CenterScreen">
    <Window.DataContext>
        <MainViewModel:MainWindowViewModel/>
    </Window.DataContext>

    <Grid>

        <StackPanel Grid.Row="0"  HorizontalAlignment="Left" VerticalAlignment="Top" Orientation="Horizontal" Height="28" Width="auto" Margin="5,0,0,0">
            <ComboBox Width="360" Margin="1,0" ItemsSource="{Binding AccountTypes}" DisplayMemberPath="Code" SelectedValuePath="ID" SelectedItem="{Binding SelectedAccountType, Mode=TwoWay}" TabIndex="0" />
        </StackPanel>

        <ContentPresenter Content="{Binding CurrentViewModel}">
        <ContentPresenter.Resources>
                <DataTemplate DataType="{x:Type ViewModel:AC1ViewModel}">
                    <Views:AC1View/>
            </DataTemplate>
                <DataTemplate DataType="{x:Type ViewModel:AC2ViewModel}">
                    <Views:AC2View/>
            </DataTemplate>
        </ContentPresenter.Resources>
    </ContentPresenter>
        </Grid>

MainWindowViewModel.cs
  public object CurrentViewModel
        {
            get { return m_currentViewModel; }
            set
            {
                m_currentViewModel = value;
                OnPropertyChanged("CurrentViewModel");    
            }
        }    
            public AccountType SelectedAccountType
            {
                get
                {
                    return m_selectedSearchAccountType;
                }
                set
                {
                    m_selectedSearchAccountType = value;
                      if (SelectedAccountType.Code == "AC1")
                    {
                        m_currentViewModel = new AC1ViewModel();
                    }
                    else if (SelectedAccountType.Code == "AC2")
                    {
                        m_currentViewModel = new AC2ViewModel();
                    }
                }
            }

谢谢。

最佳答案

从您的代码中我可以看出,您从不使用 CurrentViewModel属性(property),而不是您评估 m_currentViewModel私有(private)成员(member)。所以OnPropertyChanged("CurrentViewModel")永远不会被触发,并且您的 View 不会收到有关 CurrentViewModel 更改的通知。

因此,在您的 SelectedAccountType 属性中尝试设置 CurrentViewModel :

public AccountType SelectedAccountType
{
    get
    {
        return m_selectedSearchAccountType;
    }
    set
    {
        m_selectedSearchAccountType = value;
        if (SelectedAccountType.Code == "AC1")
        {
            CurrentViewModel = new AC1ViewModel();
        }
        else if (SelectedAccountType.Code == "AC2")
        {
            CurrentViewModel = new AC2ViewModel();
        }
    }
}

关于c# - 用户控件内容未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38702581/

相关文章:

c# - 已进行选择时卡住列表框项目的选择

c# - (Windows 10 UWP) 如何将PivotItem 控件中的Header 属性绑定(bind)到Pivot 控件中的自定义HeaderTemplate?

wpf - 为什么我需要 SilverLight 中的 ContentPresenter?

wpf - 不同用户控件中的WPF命令处理

c# - 使用 Json.Net 反序列化 Json 对象

c# - 返回一个包含泛型列表的类

wpf - WPF如何设置contentcontrol的数据模板?

wpf - 如何为绑定(bind)到 viewmodel 属性的 WPF 控件设置动画?

design-patterns - 在 MV* 模式中,模型到底是什么?

c# - Xamarin Forms MVVM 与实际模型