简体   繁体   中英

C# - Fill TextBox from WPF DataGrid

I have a WPF window with some text boxes and a DataGrid. The DataGrid gets filled with Data, but I need that, when the user clicks a cell in that DataGrid, the program detects the line and re-fills the text boxes with the data in that line. For example, there is an ID, Name and BirthDate textbox. When the user clicks any cell in a given line, the text boxes ID, Name and BirthDate's values must become the values of their respective columns (ID, Name, BirthDate) in the line selected. I've looked around for an answer to this, but I only seem to find answers relating to WinForms, and the DataGrid in WinForms and in WPF work quite differently, code-wise.

You can simply use Binding using the ElementName property to do this for you... no code behind needed:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <DataGrid Name="DataGrid" ItemsSource="{Binding YourDataCollection}" />
    <Grid Grid.Column="1">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Grid.Row="0" Text="{Binding SelectedItem.Id, 
            ElementName=DataGrid}" />
        <TextBlock Grid.Row="1" Text="{Binding SelectedItem.Name, 
            ElementName=DataGrid}" />
        <TextBlock Grid.Row="2" Text="{Binding SelectedItem.BirthDate, 
            ElementName=DataGrid}" />
    </Grid>
</Grid>

Here I made a tiny illustration following your original example:

//XAML
     <Grid>
           <DataGrid ItemsSource="{Binding Persons}" Margin="0,0,136,0" SelectedItem="{Binding SelectedPerson}"></DataGrid>
            <Label Content="{Binding SelectedPerson.Id}"  HorizontalAlignment="Left" Margin="400,35,0,0" VerticalAlignment="Top" Width="90" Height="26"/>
            <Label Content="{Binding SelectedPerson.Name}" HorizontalAlignment="Left" Margin="400,97,0,0" VerticalAlignment="Top" Width="90" Height="24"/>
            <Label Content="{Binding SelectedPerson.BirthDate}" HorizontalAlignment="Left" Margin="400,66,0,0" VerticalAlignment="Top" Width="90" Height="26"/>
        </Grid>
//.cs
     public MainWindow()
        {
            InitializeComponent();
            DataContext = new PersonViewModel();
       }
//ViewModel
    public class PersonViewModel : INotifyPropertyChanged
        {
            private Person _selectedPerson;

            public List<Person> Persons { get; set; }

            public Person SelectedPerson
            {
                get { return _selectedPerson; }
                set { _selectedPerson = value; OnPropertyChanged("SelectedPerson"); }
            }

            public PersonViewModel()
            {
                Persons = new List<Person>
                {
                    new Person(){Id = 1,BirthDate = DateTime.Now.AddYears(-30),Name = "Mark"},
                    new Person(){Id = 2,BirthDate = DateTime.Now.AddYears(-40), Name = "Sophy"},
                    new Person(){Id = 3,BirthDate = DateTime.Now.AddYears(-50), Name = "Bryan"},
                };
            }




            public event PropertyChangedEventHandler PropertyChanged;

            [NotifyPropertyChangedInvocator]
            protected virtual void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

// Model     

    public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public DateTime BirthDate { get; set; }
        }

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