Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 无法理解如何指定Datacontext_Wpf_Mvvm - Fatal编程技术网

Wpf 无法理解如何指定Datacontext

Wpf 无法理解如何指定Datacontext,wpf,mvvm,Wpf,Mvvm,在mMaterialCentreGroupListView.xaml文件中,这一行是为MaterialCentreGroupView编写的。但我看不到访问viewmodel属性的任何数据上下文。它工作得很好。下面是代码 <vw:MaterialCentreGroupView Style="{StaticResource DetailedViewStyle}" DataContext="{Binding SelectedMaterialCent

mMaterialCentreGroupListView.xaml文件中,这一行是为
MaterialCentreGroupView
编写的。但我看不到访问viewmodel属性的任何数据上下文。它工作得很好。下面是代码

<vw:MaterialCentreGroupView
            Style="{StaticResource DetailedViewStyle}"
            DataContext="{Binding SelectedMaterialCentreGroup}"/>

景色

<UserControl x:Class="MyBooks.View.MaterialCentreGroupView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
MinWidth="290" MaxWidth="290" Padding="20"
Background="#eee">

<UserControl.Resources>
    <ResourceDictionary Source="ViewResources.xaml" />
</UserControl.Resources>

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Label
        Grid.Row="0" Grid.Column="0"
        Content="{Resx ResxName=MyBooks.Properties.Strings, Key=AccountCategoryListView_Name_Label}"
        HorizontalAlignment="Right"/>
    <TextBox
        x:Name="CategoryName"
        MaxLength="50"
        Style="{StaticResource FormControlStyle}"
        Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
        Text="{Binding Path=Name, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>


请向我解释发生了什么以及绑定是如何发生的?

我假设DataContext是在代码隐藏中设置的(文件名与.xaml文件相同,但扩展名为.cs)。查找类似DataContext=newsomeviewmodel()的代码

或者,如果此UserControl正在另一个控件中使用,则可以在其上显式设置DataContext,或者,如果未在XAML或代码隐藏中显式设置DataContext,则可以从其父控件继承DataContext(可以从其父控件继承,也可以不从其父控件继承,依此类推)。

查看“Resx”绑定

<Label 
    Grid.Row="0" Grid.Column="0" 
    Content="{Resx ResxName=MyBooks.Properties.Strings, Key=AccountCategoryListView_Name_Label}" 
    HorizontalAlignment="Right"/> 

有关resx绑定和用法的更多信息,请参阅下面的链接

@StellerEleven:在MaterialCenterGroupListView.xaml中指定此行,并在viewmodel public Material Center GroupViewModel SelectedMaterial CentreGroup{return\u SelectedMaterial CentreGroup;}中指定。谢谢