C# WPF数据绑定在错误的DataContext中搜索

C# WPF数据绑定在错误的DataContext中搜索,c#,wpf,xaml,mvvm,data-binding,C#,Wpf,Xaml,Mvvm,Data Binding,我正在学习MVVM模式,在数据绑定方面遇到了一些问题。我理解它是如何工作的,但在我的示例中,它使用了错误的DataContext来搜索绑定 我没有找到另一个适合我问题的问题 因此,我有这样的看法: 使用以下ViewModel: 使用系统; 使用System.Collections.Generic; 使用System.Collections.ObjectModel; 使用System.Linq; 使用系统文本; 使用System.Threading.Tasks; 使用Kenshinaro.ca

我正在学习MVVM模式,在数据绑定方面遇到了一些问题。我理解它是如何工作的,但在我的示例中,它使用了错误的DataContext来搜索绑定

我没有找到另一个适合我问题的问题

因此,我有这样的看法:


使用以下ViewModel:

使用系统;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Kenshinaro.cashrister.UI.View.cashrister;
命名空间Kenshinaro.cashrister.UI.ViewModel.cashrister
{
公共类CashRegisterChoiceViewModel:MVVM.ViewModel
{
私有ObservableCollection\u收银机=新ObservableCollection()
{
新的CashRegisterItemsControl视图()
{
DataContext=new-CashRegisterItemsControlViewModel()
{
视图=新的现金登记卡视图()
{
DataContext=new CashRegisterCardViewModel()
{
型号=新型号。收银机()
{
Name=“1”
}
}
}
}
},
新的CashRegisterItemsControl视图()
{
DataContext=new-CashRegisterItemsControlViewModel()
{
视图=新的现金登记卡视图()
{
DataContext=new CashRegisterCardViewModel()
{
型号=新型号。收银机()
{
Name=“2”
}
}
}
}
},
新的CashRegisterItemsControl视图()
{
DataContext=new-CashRegisterItemsControlViewModel()
{
视图=新的现金登记卡视图()
{
DataContext=new CashRegisterCardViewModel()
{
型号=新型号。收银机()
{
Name=“3”
}
}
}
}
}
};
公共收款机
{
get=>\u收银机;
set=>SetProperty(参考现金收银机,值);
}
}
}
(我的基本ViewModel类正在实现INotifyPropertyChanged接口)

因此,此视图显示了此视图的列表:


视图模型:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间Kenshinaro.cashrister.UI.ViewModel.cashrister
{
公共类CashRegisterItemsControlViewModel:MVVM.ViewModel
{
private View.CashRegister.CashRegisterCardView\u视图;
public View.CashRegister.CashRegisterCardView视图
{
获取=>\u视图;
set=>SetProperty(参考视图,值);
}
}
}
此视图也显示了此视图:


视图模型:

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Kenshinaro.cashrister.Model;
命名空间Kenshinaro.cashrister.UI.ViewModel.cashrister
{
公共类CashRegisterCardViewModel:MVVM.ViewModel
{
私有模式。收银机_模式;
公共模式
{
get=>\u模型;
set=>SetProperty(参考模型,值);
}
公共字符串名
{
get=>\u model.Name;
set=>\u model.Name=value;
}
}
}
现在,如果我启动应用程序,最后一个视图中文本块上的绑定不会出现。 VisualStudio的输出显示:

System.Windows.Data错误:40:BindingExpression路径错误:“在对象”“CashRegisterItemsControlViewModel”“(HashCode=35528341)”上找不到“Name”属性。BindingExpression:Path=DataContext.Name;DataItem='CashRegisterCardView'(名称='');目标元素为“TextBlock”(名称=“”);目标属性为“Text”(类型为“String”)

正如我所知,它正在CashRegisterItemsControlView的DataContext中搜索属性,但我不明白为什么,因为我在集合初始化时手动设置了DataContext(仅用于测试):

private ObservableCollection\u收银机=新ObservableCollection()
{
新的CashRegisterItemsControl视图()
{
DataContext=new-CashRegisterItemsControlViewModel()
{
视图=新的现金登记卡视图()
{
DataContext=new CashRegisterCardViewModel()
{
型号=新型号。收银机()
{
Name=“1”
}
}
}
}
},
...
那么为什么它仍然使用错误的数据上下文呢

现在,当我将绑定更改为View.DataContext.Name时,它将在手动设置的DataContext中搜索:

System.Windows.Data错误:40:BindingExpression路径错误:“在”“对象”“CashRegisterCardView'(名称=“”)”上找不到“视图”属性。BindingExpression:path=View.DataContext.Name;DataItem='CashRegisterCardView'(名称=“”);目标el
<UserControl x:Class="Kenshinaro.CashRegister.UI.View.CashRegister.CashRegisterItemsControlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Kenshinaro.CashRegister.UI.View.CashRegister"
             xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <md:Card Margin="10">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <local:CashRegisterCardView Padding="5"/>
            <Button Margin="20" Grid.Row="1" Content="Pick me"/>
        </Grid>
    </md:Card>
</UserControl>
<UserControl x:Class="Kenshinaro.CashRegister.UI.View.CashRegister.CashRegisterItemsControlView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:Kenshinaro.CashRegister.UI.View.CashRegister"
             xmlns:md="http://materialdesigninxaml.net/winfx/xaml/themes"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <md:Card Margin="10">
        <Grid >
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <ContentControl Content="{Binding View}" Padding="5"/>
            <Button Margin="20" Grid.Row="1" Content="Pick me"/>
        </Grid>
    </md:Card>
</UserControl>