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

kee_ 2019-04-21 09:03:17
在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();
}
...全文
1274 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
kee_ 2019-04-22
  • 打赏
  • 举报
回复
问题已经解决了,因为在v的后台中给ContentControl的content绑定了一个值,因为这个值导致vm中的切换无效的,lz需要重新学习下MVVM了
exception92 2019-04-22
  • 打赏
  • 举报
回复
ViewModel = _vMQueryPayListCtrl这些东西都无效 -》都不清楚“这些东西”是干什么用的么? 既然不知道 就要重新学一下MVVM了。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧