C#wpf,从文本框绑定到dataGridColumn

时间:2015-10-02 01:40:32

标签: c# wpf mvvm binding datagrid

我仍然是wpf-mvvm的初学者。我有一个与集合绑定的数据网格。

我需要按文本框的内容填充第一列。 因此,每次添加新行时,第一列应该已经包含了我的文本框的内容。

我该怎么做?

查看:

<Grid DataContext="{Binding Source={StaticResource invoice}}">
    <StackPanel Orientation="Horizontal">
        <TextBox Width="71"
                 Name="InvoiveNumber"
                 Text="{Binding ??, 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>

视图模型:

public class InvoiceViewModel : ViewModelBase
{
    public Context ctx = new Context();

    public InvoiceViewModel()
    {
        Get(false);
    }


    private ObservableCollection<Invoice> collection;

    public ObservableCollection<Invoice> Collection
    {
        get
        {
            return collection;
        }
        set
        {
            collection = value;
            OnPropertyChanged("Collection");
        }
    }


    private Invoice _selected;
    public Invoice Selected
    {
        get
        {
            return _selected;
        }
        set
        {
            _selected = value;
            OnPropertyChanged("Selected");
        }
    }

    private void Get(bool loadDataFirst)
    {
        if (loadDataFirst) ctx.Invoices.Load();
        Collection = ctx.Invoices.Local;
    }


    private void Save()
    {
        ctx.SaveChanges();
    }

    private void Delete()
    {
        var id = Selected;
        var invoice = (from i in ctx.Invoices
                    where i.idInvoice == id.idInvoice
                    select i).SingleOrDefault();

        Collection.Remove(invoice);
    }

    private Invoice _currentItem;
    public Invoice CurrentItem
    {
        get
        {
            return _currentItem;
        }
        set
        {
            _currentItem = value;
            OnPropertyChanged("CurrentItem");
        }
    }


    #region "Command"

    private ICommand saveCommand;
    private ICommand removeCommand;

    public ICommand SaveCommand
    {
        get
        {
            return saveCommand ?? (saveCommand = new RelayCommand(p => this.Save(), p => this.CanSave()));
        }
    }


    private bool CanSave()
    {
        return true;
    }

    public ICommand DeleteCommand
    {
        get
        {
            return removeCommand ?? (removeCommand = new RelayCommand(p => this.Delete(), p => this.CanDelete()));
        }
    }

    public bool CanDelete()
    {
        if (Selected != null)
            return true;
        else
            return false;
    }

    #endregion

}

2 个答案:

答案 0 :(得分:3)

首先,感谢这样一个具有挑战性的问题。由于这个问题,我今天大量使用DataGrid。

在花了一整天并尝试各种方法以获得完美解决方案后问题得以解决。

在文本框中输入一些数字,然后单击按钮。此数字将自动显示在任何新行中。仅仅在文本框中更改此数字不会带来任何更改,因为我没有使用该方法。必须单击按钮。我可以禁用编辑第一列,但保持原样。 此解决方案同样适用于AutoGenerateColumns =&#34; True&#34;

以下代码可以按原样使用:

<强> Window1.xaml

<Window x:Class="WpfDataControls.DataGrid.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="385.714" Width="598.872">

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="249*"/>
        <RowDefinition Height="79*"/>
    </Grid.RowDefinitions>

    <TextBox x:Name="tbSupplierInvoiceNumber" HorizontalAlignment="Left" Height="23" Margin="168,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="120"/>
    <Button Content="Get invoices" HorizontalAlignment="Left" Margin="306,11,0,0" Grid.Row="1" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    <DataGrid x:Name="dgrdInvoice" HorizontalAlignment="Left" Margin="10,10,0,0" AutoGenerateColumns="False" CanUserAddRows="True" 
              VerticalAlignment="Top" Height="229" Width="571"
              LoadingRow="dgrdInvoice_LoadingRow">
        <DataGrid.Columns>
            <!-- My column -->
            <DataGridTextColumn x:Name="dataGridTextColumn"
                            Header="Supplier Invoice Nb"
                            Binding="{Binding suppInvNumber, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                            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>
    <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Initial suppInvNumber" VerticalAlignment="Top" Width="140"/>
    <TextBlock HorizontalAlignment="Left" Margin="111,38,0,0" Grid.Row="1" TextWrapping="Wrap" Text="Change this number and click get invoices to see the change" VerticalAlignment="Top"/>

</Grid>    
   </Window>

<强> Window1.xaml.cs

using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Diagnostics;

namespace WpfDataControls.DataGrid
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {      
        public Window1()
        {
            InitializeComponent();

            dgrdInvoice.AddingNewItem += dgrdInvoice_AddingNewItem;
        }       

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ViewModel vm = new ViewModel();
            dgrdInvoice.ItemsSource = vm.InvoiceCollection;
        }

        #region Grid events
            void dgrdInvoice_LoadingRow(object sender, DataGridRowEventArgs e)
            {
               e.Row.Loaded += dgrdInvoice_Row_Loaded;                
            }

            void dgrdInvoice_Row_Loaded(object sender, RoutedEventArgs e)
            {
                DataGridRow newRow = ((DataGridRow)sender);

                if (newRow.GetIndex() == dgrdInvoice.Items.Count - 1)
                {
                    newRow.Background = new SolidColorBrush(Colors.BlanchedAlmond);
                    DependencyObject reference = newRow;

                    while (true)
                    {
                        reference = VisualTreeHelper.GetChild(reference, 0);
                        if (reference is TextBlock)
                        {
                            TextBlock f = (TextBlock)reference;
                            f.Text = Convert.ToInt32(tbSupplierInvoiceNumber.Text).ToString();
                            break;
                        }
                    }
                }
            }

            void dgrdInvoice_AddingNewItem(object sender, AddingNewItemEventArgs e)
            {
                DataGridRow newRow = (DataGridRow)dgrdInvoice.ItemContainerGenerator.ContainerFromIndex(dgrdInvoice.Items.Count - 1);
                DependencyObject reference = newRow;
                int invoiceNumber;
                while (true)
                {
                    reference = VisualTreeHelper.GetChild(reference, 0);
                    if (reference is TextBlock)
                    {
                        invoiceNumber = Convert.ToInt32(tbSupplierInvoiceNumber.Text);
                        TextBlock f = (TextBlock)reference;
                        f.Text = invoiceNumber.ToString();
                        break;
                    }
                }

                e.NewItem = new Invoice() { suppInvNumber = invoiceNumber };
            }       
        #endregion

    }

    public class ViewModel
    {
        ObservableCollection<Invoice> _invoiceCollection;
        public ObservableCollection<Invoice> InvoiceCollection { get { return _invoiceCollection; } }

        public ViewModel()
        {
            _invoiceCollection = new ObservableCollection<Invoice>();

            _invoiceCollection.Add(new Invoice() { suppInvNumber = 1, quantity=120, unitPrice=23, totalPrice=56 });
            _invoiceCollection.Add(new Invoice() { suppInvNumber = 3, quantity = 122, unitPrice = 13, totalPrice = 23 });
            _invoiceCollection.Add(new Invoice() { suppInvNumber = 4, quantity = 234, unitPrice = 10, totalPrice = 43 });
            _invoiceCollection.Add(new Invoice() { suppInvNumber = 6, quantity = 512, unitPrice = 35, totalPrice = 67 });
            _invoiceCollection.Add(new Invoice() { suppInvNumber = 7, quantity = 612, unitPrice = 3, totalPrice = 120 });
        }
    }

    public class Invoice
    {
        public int suppInvNumber { get; set; }
        public int quantity { get; set; }
        public int unitPrice { get; set; }
        public int totalPrice { get; set; }
    }
}

答案 1 :(得分:0)

我不得不改变以前的方法,因为它仅在我填充第一列时才有效。 使用以下方法,我可以填充四列。

    private void dataGridInvoice_AddingNewItem(object sender, AddingNewItemEventArgs e)
    {

                DataGridRow newRow = (DataGridRow)dataGridInvoice.ItemContainerGenerator.ContainerFromIndex(dataGridInvoice.Items.Count - 1);
                DependencyObject reference = newRow;
              //  MessageBox.Show(Convert.ToString(VisualTreeHelper.GetChildrenCount(reference)));
                string invoiceNumber = null;
                int _supplier = 0;
                int _shop = 0;
                DateTime _date = DateTime.Now;


                if (newRow != null)
                {
                    DataGridCellsPresenter presenter = GetVisualChildHelper<DataGridCellsPresenter>(newRow);

                    if (presenter == null)
                    {
                        dataGridInvoice.ScrollIntoView(newRow, dataGridInvoice.Columns[1]);
                        presenter = GetVisualChildHelper<DataGridCellsPresenter>(newRow);
                    }

                    DataGridCell cell0 = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(0);
                    DataGridCell cell1 = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(1);
                    DataGridCell cell2 = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(2);
                    DataGridCell cell3 = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(3);

                    invoiceNumber = InvoiceNumber.Text;
                    int value = Convert.ToInt32(shopComboBox.SelectedValue);
                    _shop = value;

                    value = Convert.ToInt32(supplierComboBox.SelectedValue);
                    _supplier = value;

                    _date = Convert.ToDateTime(datePicker.Text);

                    cell0.Content = invoiceNumber;
                    cell1.Content = _shop;
                    cell2.Content = _date;
                    cell3.Content = _supplier;
                }

                e.NewItem = new Invoice() { suppInvNumber = invoiceNumber, shop = _shop, supplier = _supplier, date = _date };

     }


        public static T GetVisualChildHelper<T>(Visual parent) where T : Visual
        {
            T child = default(T);
            int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
            for (int i = 0; i < numVisuals; i++)
            {
                Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
                child = v as T;
                if (child == null)
                {
                    child = GetVisualChildHelper<T>(v);
                }
                if (child != null)
                {
                    break;
                }
            }
            return child;
        }