WPF Form TextBox – DataGrid Binding Örneği

R

Rooster

only business
Super Moderator
Joined
Aug 10, 2019
Messages
3,215
Reaction score
4,728
Location
Milano
Bu yazımızda WPF Formda Binding işlemi ile ilgili basit bir örnek gerçekleştireceğiz. Örneğimizde DataGrid’ de Class ve List kullanarak bazı kayıtlar listeleyeceğiz. Daha sonra XAML tarafında Datagrid’de seçilen satırın ilgili Textbox kontrolünde Binding edilerek gösterilmesini sağlayacağız. Örneğimize geçelim.
Örneğimize ait XAML kodları:


Code:
<Window x:Class="datagridbindingtextbox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="realitycheats.com" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid >
        <Label Content="Adı"  Margin="98,24,346,263" />
        <Label Content="Soyadı"  Margin="98,61,346,225" />
        <TextBox Text="{Binding ElementName=dg1, Path=SelectedItem.AD}"   Margin="176,24,144,263"/>
        <TextBox Text="{Binding ElementName=dg1, Path=SelectedItem.SOYAD}" Margin="176,61,144,225"  />
        <DataGrid Name="dg1" Grid.Column="0" AutoGenerateColumns="False" Margin="10,124,10,10">
            <DataGrid.Columns>
                <DataGridTextColumn Header="AD" Width="*" Binding="{Binding AD}"/>
                <DataGridTextColumn Header="SOYAD" Width="*" Binding="{Binding SOYAD}"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>
XAML kodlarını incelediğimizde Textbox’ların Text özelliğine DataGrid’ de seçili olan satırların Binding edildiğini görüyoruz.
DataGrid sütunlarımızın ise aşağıda verilen C# kodlarını incelediğimizde görüleceği üzere List öğemize bağlanıyor.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace datagridbindingtextbox
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<OgrenciListe> list;
        public MainWindow()
        {
            InitializeComponent();
                list = new List<OgrenciListe>()
                {
                new OgrenciListe(){ AD="AHMET", SOYAD="CANSEVER"}, list' e ekleme
                new OgrenciListe(){ AD="ALİ", SOYAD="ER"},
                new OgrenciListe(){ AD="YUSUF", SOYAD="KOÇ"},
                new OgrenciListe(){ AD="AYŞE", SOYAD="SAYGIN"},
                };
                OgrenciListe lst = new OgrenciListe(); //Diğer ekleme yöntemi.
                lst.AD = "SALİM";
                lst.SOYAD = "USLU";
                list.Add(lst);
        }

        public class OgrenciListe //Class yapımız
        {
            public string AD { get; set; }
            public string SOYAD { get; set; }
           
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            dg1.ItemsSource = list;
        }
    }
}
 
SPAM IS FORBIDDEN!
  • SPAMMERS ARE BANNED FROM THE FORUM AND CANNOT USE ANY OF THE CHEATS
  • For example: thanks, thx, very good, asdqwe, working, ty and so on!
  • For example: Writing the same message over and over. thanks, thx and so on!
  • Copying and copying someone else's message is prohibited.
  • It is forbidden to send messages to increase the number of comments on threads that you have no knowledge of.
  • Write your own opinion when commenting!
  • If you see spam message, please let us know with the REPORT button!

Tema düzenleyici

Top Bottom