将WPF子网格绑定到父网格

时间:2016-10-31 10:25:00

标签: c# wpf mvvm-light

我有一个具有以下模型的应用程序,其中包含父类作业和子作业请求。

我正在使用mvvm灯和视图模型。表格有三个部分。一个是包含所有作业的数据网格。用户选择在视图模型中触发selectedjob的作业。

selectedjob的属性显示在文本框中,并显示子JobRequirements的数据网格。

JobRequirements绑定到selectedjob.jobRequirements。

对于现有的工作要求,一切都按预期工作。

但是,jobRequirement数据网格不会绑定到新作业。用户必须使用viewModel中的代码主动添加它。这个系统有效但有没有办法让它只使用XAML?

那是否可以添加新作业并让需求数据网格知道要添加的要求是否已添加到新作业中?

型号:

 public class Job  
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 id { get; set; }

    public string jobshortname { get; set; }  

    public  virtual ObservableCollection<JobRequirement> jobRequirements { get; set; }

}

  public class JobRequirement

{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Int64 id { get; set; }
    [ForeignKey("job")]
    public Int64 Jobid { get; set; }
     public DateTime deadline { get; set; } 
    public string comment { get; set; }
    public virtual  Job job { get; set; }
    public virtual  Requirement requirement { get; set; }
}

查看型号:

  public class jobViewModel : ViewModelBase
{
    private DataService _ds;
    public const string jobscPropertyName = "jobsc";
    public const string jobrequirementsPropertyName = "requirements";
    private ObservableCollection<Requirement> _requirements ;
    public ObservableCollection<Requirement> requirements
    {
        get
        {
            return _requirements;
        }
        set
        {
            if (_requirements == value)
            {
                return;
            }
            _requirements = value;
            RaisePropertyChanged(jobrequirementsPropertyName);
        }
    }

   private ObservableCollection<Job> _jobsc;
    public ObservableCollection<Job> jobsc
    {
        get { return _jobsc; }
        set
        {
            if (_jobsc == value)
            {
                return;
            }
            _jobsc = value;
            RaisePropertyChanged(jobscPropertyName);
        }
    }
    public const string selectedJobPropertyName = "selectedJob";
    private Job _selectedJob ;
    public Job selectedJob
    {
        get
        {
            return _selectedJob;
        }
        set
        {
            if (_selectedJob == value)
            {
                return;
            }
            _selectedJob = value;
            RaisePropertyChanged(selectedJobPropertyName);
        }
    }
    public RelayCommand NewJob { get; private set; }
    public RelayCommand SaveJob { get; private set; }
     public RelayCommand Newjr { get; private set; }
    public jobViewModel(DataService ds)
    {
        _ds = ds;
        NewJob = new RelayCommand(getnewjob);
        SaveJob = new RelayCommand(savejob);
        Newjr = new RelayCommand(addRequirement);
        requirements = _ds.getAllRequirements();
        jobsc = ds.GetAllJobs();     
    }
    private void addRequirement()
    {
        JobRequirement jr = new JobRequirement();
        jr.Jobid = selectedJob.id;
        jr.deadline = DateTime.Today.AddMonths(1);
        selectedJob.jobRequirements.Add(jr);
    }
  private async void savejob()
    {   
        selectedJob = await _ds.saveJob(selectedJob);
    }
    private void getnewjob()
    {
       selectedJob = new Models.Job();
        jobsc.Add(selectedJob);
    }
}

以下是相关的XAML

  <DockPanel LastChildFill="True" Grid.Row="1" Margin="10,0,10,0">
    <DataGrid x:Name="jobDataGrid" FlowDirection="RightToLeft" DockPanel.Dock="Left"
              AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding jobsc}" Grid.Row="1" 
              SelectedItem="{Binding selectedJob, Mode=TwoWay}"  RowDetailsVisibilityMode="VisibleWhenSelected" CanUserAddRows="False"
       >         
        <DataGrid.Columns>

                <DataGridTextColumn x:Name="idColumn" Binding="{Binding id}" Header="id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="jobshortnameColumn" Binding="{Binding jobshortname}" Header="jobshortname" Width="SizeToHeader"/>
           <DataGrid.Columns>
    </DataGrid>
        <Grid x:Name="jrframe" DockPanel.Dock="Right">
            <Grid.RowDefinitions>
                <RowDefinition Height="20"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Grid.Row="0">
         <--Button to add new requirement -->
                <Button x:Name="jrbutton" Content="New" Command="{Binding Newjr}"/>
            </StackPanel>
<-- Requirement Grid -->
    <DataGrid x:Name="jobRequirementsDataGrid"  Grid.Row="1" CanUserAddRows="False"
              AutoGenerateColumns="False" EnableRowVirtualization="True" 
              ItemsSource="{Binding selectedJob.jobRequirements, Mode=TwoWay}"   RowDetailsVisibilityMode="VisibleWhenSelected">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="commentColumn" Binding="{Binding comment}" Header="comment" Width="SizeToHeader"/>
            <DataGridTemplateColumn x:Name="deadlineColumn" Header="deadline" Width="SizeToHeader">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <DatePicker  SelectedDate="{Binding deadline, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTextColumn x:Name="idColumn1" Binding="{Binding id}" Header="id" Width="SizeToHeader"/>
            <DataGridTextColumn x:Name="JobidColumn" Binding="{Binding Jobid}"  />

           </DataGrid.Columns>
    </DataGrid>
        </Grid>
        <StackPanel FlowDirection="RightToLeft">
            <Grid x:Name="grid1" DataContext="{Binding selectedJob, Mode=TwoWay}" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>                   
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>                  
                </Grid.RowDefinitions>
                <TextBox x:Name="idTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding id, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
               <Label Content="jobshortname:" Grid.Column="2" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
                <TextBox x:Name="jobshortnameTextBox" Grid.Column="3" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding jobshortname, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
                          </Grid>
        </StackPanel>


    </DockPanel>

1 个答案:

答案 0 :(得分:0)

尝试将绑定要求网格项目源绑定到selectedJob而不是selectedJob.jobRequirements。

<-- Requirement Grid -->
<DataGrid x:Name="jobRequirementsDataGrid"  Grid.Row="1" CanUserAddRows="False"
          AutoGenerateColumns="False" EnableRowVirtualization="True" 
          ItemsSource="{Binding selectedJob, Mode=TwoWay}"   RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="commentColumn" Binding="{Binding jobRequirements.comment}" Header="comment" Width="SizeToHeader"/>
        <DataGridTemplateColumn x:Name="deadlineColumn" Header="deadline" Width="SizeToHeader">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DatePicker  SelectedDate="{Binding jobRequirements.deadline, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn x:Name="idColumn1" Binding="{Binding jobRequirements.id}" Header="id" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="JobidColumn" Binding="{Binding jobRequirements.Jobid}"  />

       </DataGrid.Columns>
</DataGrid>