wpf 如何防止绑定到文本框的Object属性更改datagrid的CollectionViewSource?

up9lanfz  于 2023-06-24  发布在  其他
关注(0)|答案(1)|浏览(107)

我有绑定到文本框文本属性的MachineViewModel的EraNumber属性,以及绑定到datagrid的MachineViewModel的CollectionViewSource。每当更改文本框的值时,绑定到datagrid的集合视图源的每个Item都会更改为文本框的值。

>
<UserControl.DataContext>
    <Vm:MachineViewModel/>
</UserControl.DataContext>
<Grid x:Name="vehiclesGrid" Width="390" ShowGridLines="false"  >
   
    <Grid.RowDefinitions>
        <RowDefinition/>
       
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>

    <Label x:Name="lblVehicleEra" Content="Era Number"/>
      <TextBox x:Name="txtVehicleEra" Grid.Column="2"   PreviewTextInput="numbersOnly" Text="{Binding Era, NotifyOnTargetUpdated=True, NotifyOnValidationError=True, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Style="{StaticResource TextError}" messenger:CommandBehavior.RoutedEventName="LostFocus" messenger:CommandBehavior.TheCommandToRun="{Binding EraNumberChangedCommand}" />
<DataGrid  x:Name="dataDisplayReport"  
                           EnableColumnVirtualization="True" 
                           EnableRowVirtualization="True" 
                           HorizontalScrollBarVisibility="Auto" 
                           HorizontalAlignment="Stretch" 
                     Grid.ColumnSpan="2" Width="Auto" 
                           AutoGenerateColumns="True"
                       ItemsSource="{Binding Source={StaticResource cvsMachines}}"
                        CanUserAddRows="False"  
                           >
                    <DataGrid.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Update" Command="{Binding UpdateMachine}"/>
                        </ContextMenu>
                    </DataGrid.ContextMenu>
                   
            
                </DataGrid>
62lalag4

62lalag41#

BindingMode设置为OneWay应该可以。

<TextBox Text="{Binding Era, Mode=OneWay}" />

这里有更多关于绑定模式的信息。

相关问题