Dynamic View of DataGrid

XAML:

<Window x:Class="PropertyGridDemo.Test"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:pg="clr-namespace:Syncfusion.Windows.PropertyGrid;assembly=Syncfusion.PropertyGrid.Wpf"        
         xmlns:dg="clr-namespace:Syncfusion.Windows.Controls.Grid;assembly=Syncfusion.Grid.Wpf" 
        Title="Test" Height="500" Width="800">
    <Grid>
        <Grid.RowDefinitions >
            <RowDefinition ></RowDefinition>
            <RowDefinition ></RowDefinition>
            <RowDefinition ></RowDefinition>
        </Grid.RowDefinitions>
        <pg:PropertyGrid 
            Grid.Row="2" 
            Name="pg" 
            PropertyExpandMode="NestedMode"
            EnableGrouping="True" 
            SelectedObject="{Binding ElementName=lb,Path=SelectedItem,Mode=TwoWay}" ></pg:PropertyGrid>
        <ListBox Name="lb" Grid.Row="1" ItemsSource="{Binding ElementName=dg,Path=VisibleColumns,Mode=TwoWay}" DisplayMemberPath="MappingName"></ListBox>
        <dg:GridDataControl
            Name="dg" 
            AutoPopulateColumns="True" 
            ItemsSourceChanged="dg_ItemsSourceChanged" 
            Grid.Row="0"></dg:GridDataControl>
    </Grid>
</Window>

Backend code:

 public partial class Test : Window
    {
        public Test()
        {
            InitializeComponent();
            this.dg.ItemsSource = Data.Gets();

                  
        }
}

 public class Data
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool IsMale { get; set; }

        public static IEnumerable Gets()
        {
            List<Data> data = new List<Data>();
            for (int i = 0; i < 100; i++)
                data.Add(new Data { ID = i, Name = "Name" + i, IsMale = i % 3 == 2 });
            return data;
        }
    }

  

Screenshot:

原文地址:https://www.cnblogs.com/mjgb/p/2772235.html