我是 mvvm 模式的新手,当我尝试将属性绑定到文本框时,我总是在输出调试器中收到此错误: “System.Windows.Data Error: BindingExpression path error: 'MainView' property not found on 'DesktopContainerViewModel' 'System.String' (HashCode=-115914272). BindingExpression: Path='MainView' DataItem='DesktopContainerViewModel' (HashCode=-115914272); 目标元素是 'System.Windows.Controls.TextBlock' (Name=' '); 目标属性是'Text'(类型'System.String').."
Xaml Code :
<UserControl x:Class="PlanNetApp.UI.DesktopContainerView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:b="clr-namespace:PlanNetApp.Controls.Behaviors;assembly=PlanNetApp.Controls"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ctrl="clr-namespace:PlanNetApp.Controls.Custom;assembly=PlanNetApp.Controls"
xmlns:conv="clr-namespace:PlanNetApp.Controls.Convertors;assembly=PlanNetApp.Controls"
xmlns:v="clr-namespace:PlanNetApp.UI.Views"
xmlns:vb="clr-namespace:PlanNetApp.UI.Views.Boq"
xmlns:ctrlui="clr-namespace:PlanNetApp.UI.Controls"
xmlns:vp="clr-namespace:PlanNetApp.UI.Views.Project"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:m="clr-namespace:PlanNetApp.UI"
mc:Ignorable="d"
DataContext="DesktopContainerViewModel"
d:DataContext="{d:DesignData Source=C:\\RASEMP_Code\\DROP2\\Desktop\\DesktopV1\\PlanNetTestApp\\PlanNetApp.UI\\DesignTime\\DesktopContainerSample.xaml}"
d:DesignHeight="600" d:DesignWidth="800"
FlowDirection="LeftToRight"
>
<UserControl.Resources>
<conv:BooleanToBusyIndicatorStatus x:Key="BooleanToBusyIndicatorStatusConver"/>
<conv:IsEqualParameterToVisibility x:Key="IsEqualParameterToVisibilityConverter"/>
<conv:IsEqualParameterToBool x:Key="IsEqualParameterToBool" />
<conv:EnumValues x:Key="EnumValuesConverter" />
</UserControl.Resources>
<Grid x:Name="LayoutRoot" >
<Grid.Background>
<LinearGradientBrush StartPoint="0.5, 0" EndPoint="0.5,1">
<GradientStop Color="#FF4D4D4D" Offset="0"/>
<GradientStop Color="#FF2B2B2B" Offset="0.599"/>
<GradientStop Color="#FF0B0B0B" Offset="1"/>
</LinearGradientBrush>
</Grid.Background>
<Rectangle
b:Texture.ImageSource="../Images/Textures/Wood.png"
Opacity="0.485"
/>
<Grid x:Name="main" >
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Height="50" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="20"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ctrl:HoverText Text="Exit"
ForGroundText="#9d9c9c"
Grid.Column="1"></ctrl:HoverText>
<ctrl:HoverText Text="Somebody"
ForGroundText="#9d9c9c"
Grid.Column="3"></ctrl:HoverText>
<ctrl:HoverText Text="Support"
ForGroundText="#9d9c9c"
Grid.Column="5"></ctrl:HoverText>
*<TextBlock Text="{Binding Path=MainView}" Grid.Column="7"/>*
</Grid>
</Grid>
</Grid>
</UserControl>
后面没有代码(mvvm 模式:)) vieModel 看起来像:
视图模型.cs:
public enum DesktopMainView
{
None,
Desktop,
Apps
}
public enum AppMainView
{
None,
Projects,
Prices
}
public class DesktopContainerViewModel : AnnotatedViewModel
{
#region MView property
public const String MViewPropertyName = "MView";
private DesktopMainView p_MView;
public DesktopMainView MView
{
get
{
return p_MView;
}
set
{
if (p_MView != value)
{
p_MView = value;
_notifyPropertyChanged(MViewPropertyName);
}
}
}
#endregion
#region MainView property
public const String MainViewPropertyName = "MainView";
private String p_MainView;
public String MainView
{
get
{
return p_MainView;
}
set
{
if (p_MainView != value)
{
p_MainView = value;
_notifyPropertyChanged(MainViewPropertyName);
}
}
}
#endregion
#region AppMainView property
public const String AppMainViewPropertyName = "AppMainView";
private AppMainView p_AppMainView;
public AppMainView AppMainView
{
get
{
return p_AppMainView;
}
set
{
if (p_AppMainView != value)
{
p_AppMainView = value;
_notifyPropertyChanged(AppMainViewPropertyName);
}
}
}
#endregion
#region NavigateToProjectDetailsCommand property
public const String NavigateToProjectDetailsCommandPropertyName = "NavigateToProjectDetailsCommand";
private RelayCommand p_NavigateToProjectDetailsCommand;
public RelayCommand NavigateToProjectDetailsCommand
{
get
{
return p_NavigateToProjectDetailsCommand;
}
set
{
if (p_NavigateToProjectDetailsCommand != value)
{
p_NavigateToProjectDetailsCommand = value;
_notifyPropertyChanged(NavigateToProjectDetailsCommandPropertyName);
}
}
}
#endregion
private void ExecuteNavigateToProjectDetailsCommand(object param)
{
FrameworkElement anchor = param as FrameworkElement;
if (MView == DesktopMainView.Desktop)
{
MView = DesktopMainView.Apps;
AppMainView = AppMainView.Projects;
}
}
public DesktopContainerViewModel()
{
NavigateToProjectDetailsCommand = new RelayCommand(ExecuteNavigateToProjectDetailsCommand);
MView = DesktopMainView.Desktop;
AppMainView = AppMainView.None;
MainView = MView.ToString();
}
public override void Cleanup()
{
throw new NotImplementedException();
}
protected override void OnAnnotationDirty()
{
throw new NotImplementedException();
}
public override string AnnotationTitle()
{
throw new NotImplementedException();
}
}
在我的 viemodel 中有一个名为 MainView 的属性!此外,我正在使用 designTime 绑定,并且通过此绑定,我可以看到绑定有效.. 可能是什么问题?