WPF中用MVVM绑定ContentControl界面无变化

kee_ kee_ 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在WPF中使用MVVM绑定ContentControl,调试的时候Query和Refund方法都是进的去的,问题就在
ViewModel = _vMQueryPayListCtrl这些东西都无效,界面没有变化,仔细对比了别人的demo,也没找到问题。
VMMainWindow中继承了INotifyPropertyChanged,由于代码较多,只贴了主要部分代码。
xaml部分
 <Window.Resources>
<v:VMMainWindow x:Key="MainViewModel" />
<DataTemplate DataType="{x:Type v:VMNumberArea}">
<c:NumberArea />
</DataTemplate>
<DataTemplate DataType="{x:Type v:VMQueryPayListCtrl}">
<c:QueryPayListCtrl/>
</DataTemplate>
<DataTemplate DataType="{x:Type v:VMRefundCtrl}">
<c:RefundCtrl/>
</DataTemplate>

</Window.Resources>
<Grid DataContext="{StaticResource MainViewModel}">
<ContentControl x:Name="workArea" Background="White" Content="{Binding ViewModel}" HorizontalAlignment="Left" Height="{Binding Wheight}" Width="850" Grid.Row="2" VerticalAlignment="Top"/>


        <UserCtrl:ShadeCtrl  Tag="查询" HorizontalAlignment="Left" Grid.Row="0" VerticalAlignment="Top" Height="85" Margin="131,0,0,0" Width="58">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding TextBlockCommand}" CommandParameter="查询" />
</i:EventTrigger>
</i:Interaction.Triggers>
</UserCtrl:ShadeCtrl>
<UserCtrl:ShadeCtrl Tag="退款" HorizontalAlignment="Left" Grid.Row="0" VerticalAlignment="Top" Height="85" Margin="63,0,0,0" Width="68" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding TextBlockCommand}" CommandParameter="退款" />
</i:EventTrigger>
</i:Interaction.Triggers>
</UserCtrl:ShadeCtrl>


VM部分
 
private readonly VMNumberArea _vMNumberArea = new VMNumberArea();
private readonly VMQueryPayListCtrl _vMQueryPayListCtrl = new VMQueryPayListCtrl();
private readonly VMRefundCtrl _vMRefundCtrl = new VMRefundCtrl();
private object _viewModel;

public object ViewModel
{
get { return _viewModel; }
set
{
if (_viewModel == value)
{
return;
}
_viewModel = value;
OnPropertyChanged("ViewModel");
}
}
public DelegateCommand TextBlockCommand { get; set; }

public VMMainWindow()
{
this.TextBlockCommand = new DelegateCommand();
this.TextBlockCommand.ExecuteAction = new Action<object>(ShowTextBlock);
}

private void ShowTextBlock(object obj)
{
switch (obj.ToString())
{
case "收款":
Receivables();
break;
case "退款":
Refund();
break;
case "查询":
Query();
break;
case "统计":
Statistics();
break;
case "解绑":
Untying();
break;
case "设置":
Setup();
break;
case "账户":
Account();
break;
case "退出":
default:
Closed?.Invoke();
break;
}
}
private void Query()
{
Height = new GridLength(0);
Wheight = 475;
ViewModel = _vMQueryPayListCtrl;
}

private void Refund()
{
ViewModel = new VMRefundCtrl();
}
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
kee_
C# 111,129 成员 642,539 内容