Home > Software design >  What is the code-behind equivalence of XAML <DataGrid Name="myDataGrid"/>?
What is the code-behind equivalence of XAML <DataGrid Name="myDataGrid"/>?

Time:01-26

Can I add WPF items programmatically without any manual interference with XAML? Please give me a hand.

CodePudding user response:

learning xaml markup is necessary step in wpf development, but yes, wpf app can be written purely in c#:

public class MainWindow : Window
{
    DataGrid myDataGrid;

    public MainWindow()
    {
        InitializeComponent();

        var root = new Grid();

        myDataGrid = new DataGrid();
        var items = new ObservableCollection<DgItem> { new DgItem { Name = "A" } };
        myDataGrid.ItemsSource = items;

        root.Children.Add(myDataGrid);
        this.Content = root;
    }
}

public class DgItem
{
    public string Name { get; set; }
}

CodePudding user response:

There is no "equivalent expression" in code behind. By naming your datagrid you are creating a property on your class that can be accessed in your code-behind and elsewehere.

  •  Tags:  
  • Related