C# - 从 WPF DataGrid 填充 TextBox

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19390904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:52:09  来源:igfitidea点击:

C# - Fill TextBox from WPF DataGrid

c#wpfdatagridtextbox

提问by chaosflaw

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.

我有一个带有一些文本框和 DataGrid 的 WPF 窗口。DataGrid 填充了数据,但我需要的是,当用户单击该 DataGrid 中的单元格时,程序会检测到该行并用该行中的数据重新填充文本框。例如,有一个 ID、姓名和出生日期文本框。当用户单击给定行中的任何单元格时,文本框 ID、Name 和 BirthDate 的值必须成为所选行中它们各自列(ID、Name、BirthDate)的值。我四处寻找答案,但我似乎只找到了与 WinForms 相关的答案,而 WinForms 和 WPF 中的 DataGrid 在代码方面的工作方式大不相同。

回答by Sheridan

You can simply use Bindingusing the ElementNameproperty to do this for you... no code behind needed:

您可以简单地使用BindingusingElementName属性为您执行此操作......不需要背后的代码:

<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>

回答by HichemSeeSharp

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; }
        }