一个大视图中的几个小视图mvvm光

时间:2018-09-18 11:58:34

标签: c# asp.net mvvm mvvm-light

我是C#的新手,但我尝试拥有几个小视角,而我不得不拥有一个大视角 但是我的数据绑定不起作用。 我有使用mvvm light。 我在每个小视图中都添加了一个datacontex。 这是mainviewwindows的代码:

<Window xmlns:View1="clr-namespace:BBS.CaseDetails.Operator.View"  xmlns:View="clr-namespace:BBS.CaseDetails.Suspect.View"  x:Class="BBS.CaseDetails.CaseDetailsWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BBS.CaseDetails"
        xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"

        mc:Ignorable="d"
        Title="CaseDetailsWindow" Height="450" Width="800">
    <Grid d:DataContext="{Binding Source={StaticResource Locator}}">
        <View1:CaseInformationView  />
    </Grid>
</Window>

小视图的代码:

<UserControl x:Class="BBS.CaseDetails.Operator.View.CaseInformationView"
             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:BBS.CaseDetails.Operator.View"
             xmlns:resx="clr-namespace:BBS.Resource.Properties;assembly=BBS.Resource"
             mc:Ignorable="d" 
             d:DataContext="{Binding CaseInformationViewModel, Source={StaticResource Locator}}">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1.5*" />
            <ColumnDefinition Width="4*" />

        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="{x:Static resx:CaseDetails.CaseInformation}" FontWeight="Bold"/>
        <Label Grid.Row="1" Grid.Column="0" Content="{x:Static resx:CaseDetails.SteriaFitPlusOperator}"/>
        <Label Grid.Row="2" Grid.Column="0" Content="{x:Static resx:CaseDetails.IncidentNumber}"/>
        <Label Grid.Row="3" Grid.Column="0" Content="{x:Static resx:CaseDetails.IncidentDate}"/>
        <TextBox Grid.Column="1" Grid.Row="1" Text="{Binding SteriaFitPlusOperator}"  />
        <TextBox Grid.Column="1" Grid.Row="2" Text="{Binding CaseInformationModel.IncidentNumber}"  />
        <DatePicker Grid.Column="1" Grid.Row="3" Text="{Binding CaseInformationModel.IncidentDate.Date, Mode=OneWay}" />
        <Image Grid.Column="2" HorizontalAlignment="Center" Height="auto"  Grid.Row="1" Width="100" />
    </Grid>
</UserControl>

我的小视图的视图模型:

namespace BBS.CaseDetails.Operator.ViewModel
{
   public class CaseInformationViewModel: ViewModelBase
    {
        private CaseInformationModel _caseInformationModel;
        public CaseInformationModel CaseInformationModel
        {
            get
            {
                return _caseInformationModel;
            }
            set
            {
                Set(() => this.CaseInformationModel, ref _caseInformationModel, value);
            }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您的问题可能在这里:

...<Grid d:DataContext="{Binding Source={StaticResource Locator}}">...

...mc:Ignorable="d" 
   d:DataContext="{Binding CaseInformationViewModel, Source={StaticResource Locator}}">...

查看以下内容:Commenting properties in xaml