绑定到文本框和集合的Datagridcolumn

本文关键字:集合 Datagridcolumn 文本 绑定 | 更新日期: 2023-09-27 18:13:58

我还是wpf - mvvm的初学者。我有一个与集合绑定的数据网格。我想在文本框中写入的文本填充我的数据网格的第一列"InvoiceNb"的每一行。

我该怎么做呢?

        <Grid DataContext="{Binding Source={StaticResource invoice}}">
                    <StackPanel Orientation="Horizontal">
                        <TextBox Width="71" Name="InvoiveNumber" Text="{Binding DataContext.Collection/suppInvNumber, ElementName=InvoiveNumber, Mode=OneWay}"> 
<!-- My textbox -->
                        </TextBox>
                        <DatePicker>
                        </DatePicker>
                        <Label Content="Shop"/>
                        <ComboBox Margin="5" 
                                  ItemsSource="{Binding Collection, Source={StaticResource shop}}" 
                                  DisplayMemberPath="shop1" Width="53" 
                                  />
                        <Label Content="Supplier"/>
                        <ComboBox Margin="5" 
                                  ItemsSource="{Binding Collection, Source={StaticResource supplier}}" 
                                  DisplayMemberPath="supplier"
                                  SelectedItem="{Binding Selected, Source={StaticResource supplier}, Mode=TwoWay}" Width="46"/>
                    </StackPanel>
                    <DataGrid x:Name="dataGridInvoice" Margin="5" Grid.Row="1" 
                                  ItemsSource="{Binding Collection}" AutoGenerateColumns="False">
                        <DataGrid.Columns>
       <!-- My column -->         <DataGridTextColumn x:Name="dataGridTextColumn" Header="Supplier Invoice Nb" Binding="{Binding suppInvNumber, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>
                            <DataGridComboBoxColumn Header="Ref Supplier"
                                                        ItemsSource="{Binding Products, Source={StaticResource supplier}, Mode=TwoWay}"
                                                        DisplayMemberPath="refsup" 
                                                        SelectedValueBinding="{Binding refSupp}" 
                                                        SelectedValuePath="refsup"
                                                        Width="*"/>
                            <DataGridTextColumn Header="Quantity" Binding="{Binding quantity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>
                            <DataGridTextColumn Header="Prix/MOQ" Binding="{Binding unitPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>
                            <DataGridTextColumn Header="Total Price" Binding="{Binding totalPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="*"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <Button  x:Name="BtnAdd" Content="Save" Command="{Binding SaveCommand}" Margin="94,0"/>
                </StackPanel>
            </Grid>

绑定到文本框和集合的Datagridcolumn

可以直接将Text绑定到DataGrid SelectedItem:

<TextBox Width="71" Name="InvoiveNumber" 
    Text="{Binding ElementName=dataGridInvoice, Path=SelectedItem.InvoiceNb}"/>

或者在ViewModel中定义CurrentItem属性并绑定DataGrid SelectedItem,然后将TextBox的Text绑定到DataContext的CurrentItem。

<TextBox Width="71" Name="InvoiveNumber" 
    Text="{Binding CurrentItem.InvoiceNb}"/>
<DataGrid SelectedItem={Binding CurrentItem, Mode=TwoWay} .... />