Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.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
C# 如何从文本框禁用Datagrid之间的更新?_C#_Wpf_Mvvm - Fatal编程技术网

C# 如何从文本框禁用Datagrid之间的更新?

C# 如何从文本框禁用Datagrid之间的更新?,c#,wpf,mvvm,C#,Wpf,Mvvm,我有这张表格 摘要 在从Datagrid选择的记录上,正确的表单将填充文本框 问题 请注意: 当我取消选中复选框时,DataGrid也会更新 DataGrid只应在单击按钮时更新 问题 如何删除DataGrid和表单之间的更新?因此,如果我取消选中该框,该值将保持为原始值。我不需要这个自动更新 XAML <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="1.5*&

我有这张表格

摘要

在从Datagrid选择的记录上,正确的表单将填充文本框

问题

请注意:

当我取消选中复选框时,DataGrid也会更新

DataGrid只应在单击按钮时更新

问题

如何删除DataGrid和表单之间的更新?因此,如果我取消选中该框,该值将保持为原始值。我不需要这个自动更新

XAML

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

    <!--DataGrid View for record selection-->
    <Border BorderBrush="Black" BorderThickness="1" Grid.Column="0">
        <StackPanel>
            <DataGrid x:Name="FSQMGrid" 
                      ItemsSource="{Binding FSQMRecords}"
                      IsReadOnly="True"
                      AutoGenerateColumns="False"
                      Style="{StaticResource MaterialDesignDataGrid}"
                      >
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Id}" Header="Id" Visibility="Hidden"/>
                    <DataGridTextColumn Binding="{Binding DocumentReference}" Header="Document Reference"/>
                    <DataGridTextColumn Binding="{Binding DocumentTitle}" Header="Document Title"/>
                    <DataGridTextColumn Binding="{Binding Path}" Header="Path" Visibility="Hidden"/>
                    <DataGridTextColumn Binding="{Binding IssueNumber}" Header="Issue Number"/>
                    <DataGridTextColumn Binding="{Binding IssueDate}" Header="Issue Date"/>
                    <DataGridTextColumn Binding="{Binding NextReviewDate}" Header="Next Review Date"/>
                    <DataGridTextColumn Binding="{Binding Archived}" Header="Archived"/>
                    <DataGridTextColumn Binding="{Binding UserIDModified}" Header="User ID Modified" Visibility="Hidden"/>
                    <DataGridTextColumn Binding="{Binding UsernameModified}" Header="User Modified"/>
                </DataGrid.Columns>


            </DataGrid>
        </StackPanel>
    </Border>

    <!--Editing Data-->


    <StackPanel Grid.Column="1">

        <materialDesign:Card Padding="20"
                             Margin="0 0 0 0">

            <StackPanel>

                <TextBlock Margin="16 16 12 8"
                           FontSize="16">Selected Record</TextBlock>

                <Separator Style="{StaticResource MaterialDesignLightSeparator}" Background="LightGray"/>


                <CheckBox Content="Archived"
                          IsChecked="{Binding ElementName=FSQMGrid, Path=SelectedItem.Archived}"
                          Style="{StaticResource MaterialDesignAccentCheckBox}" 
                          Margin="0 0 0 0"/>

                <StackPanel Margin="0 10 0 0">

                    <TextBox materialDesign:HintAssist.Hint="Document Reference"
                             Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.DocumentReference, Mode=TwoWay}"
                             Style="{StaticResource MaterialDesignFloatingHintTextBox}"/>

                    <TextBox materialDesign:HintAssist.Hint="Document Title"
                             Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.DocumentTitle, Mode=TwoWay}"
                             Margin="0 10 0 0"
                             Style="{StaticResource MaterialDesignFloatingHintTextBox}" />

                    <TextBox materialDesign:HintAssist.Hint="Section"
                             Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.SectionNumber, Mode=TwoWay}"
                             Margin="0 10 0 0"
                             Style="{StaticResource MaterialDesignFloatingHintTextBox}" />

                    <TextBox materialDesign:HintAssist.Hint="Issue Number"
                             Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.IssueNumber, Mode=TwoWay}"
                             Margin="0 10 0 0"
                             Style="{StaticResource MaterialDesignFloatingHintTextBox}" />

                    <DatePicker materialDesign:HintAssist.Hint="Issue Date"
                                Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.IssueDate, Mode=TwoWay}"
                                Margin="0 10 0 0"
                                Style="{StaticResource MaterialDesignFloatingHintDatePicker}"/>

                    <DatePicker materialDesign:HintAssist.Hint="Review Date"
                                Text="{Binding ElementName=FSQMGrid, Path=SelectedItem.NextReviewDate, Mode=TwoWay}"
                                Margin="0 10 0 0"
                                Style="{StaticResource MaterialDesignFloatingHintDatePicker}"/>

                    <Button Command="{}"
                                Content="Save Information"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Center"
                                Margin="50,20,0,0"/>

                </StackPanel>
            </StackPanel>
        </materialDesign:Card>
    </StackPanel>
</Grid>

选定记录

您正在编辑在
数据网格中看到的同一对象的相同属性,因此无论您是否单击
按钮
,数据实际上都会更新

可以考虑在数据对象类中实现接口。它提供了向用作数据源的对象提交或回滚更改的功能


另一个选项是编辑绑定到
数据网格
中的对象的副本,然后在实际单击按钮时更新后者。

您正在编辑在
数据网格
中看到的同一对象的相同属性,因此无论是否单击
按钮

可以考虑在数据对象类中实现接口。它提供了向用作数据源的对象提交或回滚更改的功能

另一个选项是在
数据网格中编辑绑定到的对象的副本,然后在实际单击按钮时更新后者。

  • Datagrid中的绑定源和selectedRecord的绑定源是实现inotifyPropertyChanged接口的同一个对象。因此,如果在任何位置更新此对象,它将在视图上同步
  • 这是在PRISM框架中编写的按钮点击更新解决方案
像这样:

public class DataGridViewModel:ViewModel
{
    public ObservableCollection<DataGridRowItem> Source { get; }

   public ICommand SelectedCommand =>new DelegateCommand<DataGridRowItem>(item =>
   {
       var clone = new DataGridRowItem()
       {
           Archived =  item.Archived
       };
       var vm = new SelectedRecordViewModel()
       {
           Data = clone,
           SaveCommand = new DelegateCommand(() =>
           {
               item.Archived = clone.Archived;
           })
       };
       var selectedRecord = new Window();
       selectedRecord.DataContext = vm;
       selectedRecord.Show();
   });  

}

public class SelectedRecordViewModel : ViewModel
{
    public DataGridRowItem Data { get; set; }

    public ICommand SaveCommand { get; set; }
}

public class DataGridRowItem : ViewModel
{
    private bool archived;

    public bool Archived
    {
        get => archived;
        set => this.SetValue(ref archived, value);
    }


}
公共类DataGridViewModel:ViewModel { 公共ObservableCollection源{get;} 公共ICommand SelectedCommand=>newdelegateCommand(项=> { var clone=new DataGridRowItem() { 存档=项目。已存档 }; var vm=new SelectedRecordViewModel() { 数据=克隆, SaveCommand=newdelegateCommand(()=> { item.Archived=克隆.Archived; }) }; var selectedRecord=新窗口(); selectedRecord.DataContext=vm; selectedRecord.Show(); }); } 公共类SelectedRecordViewModel:ViewModel { 公共DataGridRowItem数据{get;set;} 公共ICommand SaveCommand{get;set;} } 公共类DataGridRowItem:ViewModel { 私人档案; 公共图书馆 { 获取=>存档; set=>this.SetValue(ref归档,value); } }
附注:@mm8提到的解决方案更好

  • Datagrid中的绑定源和selectedRecord的绑定源是实现inotifyPropertyChanged接口的同一个对象。因此,如果在任何位置更新此对象,它将在视图上同步
  • 这是在PRISM框架中编写的按钮点击更新解决方案
像这样:

public class DataGridViewModel:ViewModel
{
    public ObservableCollection<DataGridRowItem> Source { get; }

   public ICommand SelectedCommand =>new DelegateCommand<DataGridRowItem>(item =>
   {
       var clone = new DataGridRowItem()
       {
           Archived =  item.Archived
       };
       var vm = new SelectedRecordViewModel()
       {
           Data = clone,
           SaveCommand = new DelegateCommand(() =>
           {
               item.Archived = clone.Archived;
           })
       };
       var selectedRecord = new Window();
       selectedRecord.DataContext = vm;
       selectedRecord.Show();
   });  

}

public class SelectedRecordViewModel : ViewModel
{
    public DataGridRowItem Data { get; set; }

    public ICommand SaveCommand { get; set; }
}

public class DataGridRowItem : ViewModel
{
    private bool archived;

    public bool Archived
    {
        get => archived;
        set => this.SetValue(ref archived, value);
    }


}
公共类DataGridViewModel:ViewModel { 公共ObservableCollection源{get;} 公共ICommand SelectedCommand=>newdelegateCommand(项=> { var clone=new DataGridRowItem() { 存档=项目。已存档 }; var vm=new SelectedRecordViewModel() { 数据=克隆, SaveCommand=newdelegateCommand(()=> { item.Archived=克隆.Archived; }) }; var selectedRecord=新窗口(); selectedRecord.DataContext=vm; selectedRecord.Show(); }); } 公共类SelectedRecordViewModel:ViewModel { 公共DataGridRowItem数据{get;set;} 公共ICommand SaveCommand{get;set;} } 公共类DataGridRowItem:ViewModel { 私人档案; 公共图书馆 { 获取=>存档; set=>this.SetValue(ref归档,value); } }

注:@mm8提到的解决方案更好

您正在编辑在
DataGrid
中看到的同一对象的相同属性,因此无论您是否单击
按钮
,数据都会更新。您可能想实现
IEditableObject
。您的观点是什么?您最初的问题是关于如何在编辑显示在其中的对象时禁止更新
DataGrid
。@mm8在数据对象
FSQMRecord
或任何您称之为的对象中实现
IEditableObject
。您认为正在编辑在
DataGrid
中看到的同一对象的相同属性,因此无论是否单击
按钮,数据都会更新。您可能想要实现
IEditableObject
。您的观点是什么?您最初的问题是关于如何在编辑显示在其中的对象时禁用更新
DataGrid
。@mm8在哪里实现