简体   繁体   中英

From a textbox to a datagrid WPF C#

So I'm currently trying out some different stuff, and I want to push the text from my textbox to a datagrid. I currently have made my XAML like this:

XAML:

<Grid>
    <DataGrid HorizontalAlignment="Left" Height="160" Margin="138,54,0,0" VerticalAlignment="Top" Width="512">
        <DataGrid.Columns>
            <DataGridTextColumn Header="ID" Binding="{Binding ID}" Width="*" />
            <DataGridTextColumn Header="NAME" Binding="{Binding NAME}" Width="*" />
        </DataGrid.Columns>
    </DataGrid>
    <Label x:Name="lblID" Content="ID:" HorizontalAlignment="Left" Margin="138,239,0,0" VerticalAlignment="Top"/>
    <Label x:Name="lblNAME" Content="NAME:" HorizontalAlignment="Left" Margin="138,265,0,0" VerticalAlignment="Top"/>
    <TextBox x:Name="txtID" HorizontalAlignment="Left" Height="23" Margin="187,243,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <TextBox x:Name="txtNAAM" HorizontalAlignment="Left" Height="23" Margin="188,271,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
    <Button x:Name="btnSend" Content="Send" HorizontalAlignment="Left" Margin="233,316,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

As you could see, I already have a button named send, and I've got 2 xa textbox named ID and NAME.

So how can I add the the text from the textboxes to the datagrid with only clicking on 1 button (SEND) and how do I keep repeating it for a second line, and a third line and so on?

if you have a View model then you can bind 2 different controls to the same field

this will means that when you change the text box it will use the binding to update the backing field and then the VM will notify all controls that they need to update the value

class VM : INofityPropertyChanged
{
    private string text;
    public string Text
    {
        get=>text;
        set{
            text=value;
            PropertyChanged(this,new PropertyChangedEventArg("Text");
        }
    }
    ...
}

then use binding

<TextBox Text="{Binding Text}"/>
<Label Content="{Binding Text}"/>

this assumes you have set the VM as the data context

The primitive and straight forward way would be to simply add an event handler to the Button 's Click event and add an instance of your class to the source collection of the DataGrid in there, eg:

public partial class MainWindow : Window
{
    private readonly ObservableCollection<Model> _items = new ObservableCollection<Model>();

    public MainWindow()
    {
        InitializeComponent();
        dataGrid.ItemsSource = _items;
        btnSend.Click += BtnSend_Click;
    }

    private void BtnSend_Click(object sender, RoutedEventArgs e)
    {
        _items.Add(new Model() { ID = txtID.Text, NAME = txtNAAM.Text });
    }
}

public class Model
{
    public string ID { get; set; }
    public string NAME { get; set; }
}

If you are developing an enterprise application, you should look into refactoring your code to use a view model class in between the view (window) and the model for testability and separation of concerns reasons. This design pattern is known as MVVM (Model-View-ViewModel) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM