【问题标题】:C# - Fill TextBox from WPF DataGridC# - 从 WPF DataGrid 填充文本框
【发布时间】:2013-10-15 20:56:00
【问题描述】:

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

【问题讨论】:

  • Tutorial。你的问题对于 Stackoverflow 来说太宽泛了。顺便说一句,winforms 很烂。
  • 我知道这很糟糕,这就是我使用 WPF 的原因。我会看看你给我的那个链接。
  • 需要了解 MVVM 模式。 MVVM 将“经常”使用 WPF 让您的生活更轻松。
  • 您可以从此链接获取信息。[binding datagrid with textbox][1] [1]: stackoverflow.com/questions/3901752/…

标签: c# wpf datagrid textbox


【解决方案1】:

您可以简单地使用Binding 使用ElementName 属性为您执行此操作...无需后面的代码:

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

【讨论】:

  • 看来效果不错,非常感谢!
【解决方案2】:

这里我按照你原来的例子做了一个小插图:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多