gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:54:42 24 4
gpt4 key购买 nike

我在我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com