我的网格中的数据不会更新

时间:2016-06-17 06:30:58

标签: c# wpf mvvm observablecollection

我的视图模型中有一个可观察的集合(使用WPF c#)。

我附上'这到我的datagrid

我有一个按钮

我按下按钮,它将搜索特定行并更新视图的值。

网格中的数据未更新。

我想通过使用一个可观察的它会更新吗?

查看型号:

private Visibility _JobHistorySelected;

public ObservableCollection<InformedWorkerModel.JobProfile> JobHistory
{
    get { return _JobHistory; }
    set
    {
        _JobHistory = value; RaisePropertyChanged("JobHistory");
    }
}

相关的xaml:

<DataGrid      Name="dgJobHistory"     
        ItemsSource="{Binding  ActiveRecord.JobHistory}" 
        AutoGenerateColumns="False" 
        SelectionMode="Single"
        AlternatingRowBackground="LightBlue"
        CanUserSortColumns="True"                           
        GridLinesVisibility="Vertical" 
        CanUserAddRows="False" 
      Visibility="{Binding ActiveRecord.JobHistoryIsSelected}" 
      Grid.Row="1"
         IsReadOnly="True"
      ScrollViewer.VerticalScrollBarVisibility="Auto"
      ScrollViewer.HorizontalScrollBarVisibility="Hidden" 
      MouseDoubleClick="dgJobHistory_MouseDoubleClick"                     
      >
    <DataGrid.Columns>
        <mui:DataGridTextColumn Header="Job Id" Binding="{Binding JobId, StringFormat='0000000000'}" Width="90" IsReadOnly="True">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Margin" Value="3"></Setter>
                    <Setter Property="FontSize" Value="11"></Setter>
                    <Setter Property="Background" Value="Transparent"></Setter>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </mui:DataGridTextColumn>
        <mui:DataGridTextColumn Header="Date Booked" Binding="{Binding DateBookedFormated}" Width="120" IsReadOnly="True">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Margin" Value="3"></Setter>
                    <Setter Property="FontSize" Value="11"></Setter>
                    <Setter Property="Background" Value="Transparent"></Setter>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </mui:DataGridTextColumn>
        <mui:DataGridTextColumn Header="Job Status" Binding="{Binding JobStatus}" Width="100" IsReadOnly="True">
            <DataGridTextColumn.HeaderStyle>
                <Style TargetType="{x:Type DataGridColumnHeader}">
                    <Setter Property="Margin" Value="3"></Setter>
                    <Setter Property="FontSize" Value="11"></Setter>
                    <Setter Property="Background" Value="Transparent"></Setter>
                    <Setter Property="HorizontalAlignment" Value="Stretch"/>
                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                </Style>
            </DataGridTextColumn.HeaderStyle>
        </mui:DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>
代码背后的代码:

public void SaveNewStatus(object sender, RoutedEventArgs e)
{
    var vm = this.DataContext as ApplicationViewModel;
    InformedWorkerBusinessService.JobStatus.SaveStatus(vm.ActiveRecord.JobRecord.JobStatus, vm.ActiveRecord.JobRecord.JobRef);
    foreach (var history in vm.ActiveRecord.JobHistory)
    {
        if (history.JobRef== vm.ActiveRecord.JobRecord.JobRef)
        {
            history.JobStatus = vm.ActiveRecord.JobRecord.JobStatus;
            break;
        }
    }
}

请注意

当我检查代码隐藏时是否找到了行并且字段已更新时,它是..

感谢

1 个答案:

答案 0 :(得分:5)

ObservableCollection只是意味着该集合具有在集合中添加删除时通知的实现。如果记录的属性发生更改, 记录的属性必须包含通知UI其更改的方法。例如,您可以为集合中的项目实现INotifyPropertyChanged