Custom DataGrid with retention of the style

60 Views Asked by At

I use C# WPF and ModernWPF theme.
I want to use a CustomDataGrid (because I need multiple selection). Source: https://github.com/dotnet/wpf/issues/3140

The solution still works with ModernWPF, but the datagrid is in the "classical / old" style. Is there a way to reapply the style from ModernWPF?

minimal demo project:

MainWindow.xaml

    <Grid>
        <local:CustomDataGrid 
            AutoGenerateColumns="True" 
            ItemsSource="{Binding Data}" />
    </Grid>

ViewModel.cs

public partial class ViewModel :ObservableObject
    {
        [ObservableProperty]
        public DataTable _data;

        public ViewModel()
        {
            var currentData = new DataTable();
            currentData.Columns.Add("Name", typeof(string));
            currentData.Columns.Add("BooleanValue", typeof(bool));


            //Two rows
            DataRow firstRow = currentData.NewRow();
            DataRow secondRow = currentData.NewRow();

            firstRow[0] = "Text1";
                firstRow[1] = true;
            
            secondRow[0] = "Text2"; 
                secondRow[1] = false;

            currentData.Rows.Add(firstRow);
            currentData.Rows.Add(secondRow);

            _data = currentData;
        }
    }

and the customDataGrid:

public class CustomDataGrid : DataGrid
    {
        public CustomDataGrid()
            => SelectionChanged += CustomDataGrid_SelectionChanged;

        void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
            => SelectedItemsList = SelectedItems;
        #region SelectedItemsList

        public IList SelectedItemsList
        {
            get => (IList)GetValue(SelectedItemsListProperty);
            set => SetValue(SelectedItemsListProperty, value);
        }

        public static readonly DependencyProperty SelectedItemsListProperty =
                DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(propertyChangedCallback: null));

        #endregion
    }
0

There are 0 best solutions below