We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Bind to a SmartEnum

I am using 

Ardalis.SmartEnum for my enums but can find away to binf to a datagrid coloumn

using Ardalis.SmartEnum, i have tried AutoGenerateColumns="True" but nothing shows in the grid for the enum.

the code for enum

public class WindowFrameTypes : SmartEnum<WindowFrameTypes>

{

    public static readonly WindowFrameTypes TopHung = new(nameof(TopHung), 1, "Top Hung");

    public static readonly WindowFrameTypes BottomHung = new(nameof(BottomHung), 2, "Bottom Hung");

    public static readonly WindowFrameTypes SideHung = new(nameof(SideHung), 3, "Side Hung");

    public static readonly WindowFrameTypes VerticalPivot = new(nameof(VerticalPivot), 4, "Vertical Pivot");

    public static readonly WindowFrameTypes HorizontalPivot = new(nameof(HorizontalPivot), 5, "Horizontal Pivot");

    public static readonly WindowFrameTypes FixedLight = new(nameof(FixedLight), 6, "Fixed Light");


    public string Description { get; }


    private WindowFrameTypes(string name, int value, string description): base(name, value)

    {

        Description = description;

    }

}


Im using an 

ObservableCollection<SteelFrameViewModel> with various propertis in to bind to the datagrid, all the text and int properties show up, but none of the enum properties show in datagrid


Can you binf to a SmartEnum in your datagrid.


Regards


Tony





6 Replies 1 reply marked as answer

SJ Sathiyathanam Jeyakumar Syncfusion Team February 10, 2023 12:58 PM UTC

Hi tonyb,

We have created a sample based on the information provided, and we attempted to show the "WindowFrameTypes.Description" with the AutoGenerateColumn set to true in DataGrid. And we are able to Show the Description in DataGrid. Please review the sample and a screenshot.


ViewModel.cs

public class ViewModel

{

   public ObservableCollection<SteelFrameViewModel> SteelFrameViewModels { get; set; }

    public ViewModel()

    {

        SteelFrameViewModels = new ObservableCollection<SteelFrameViewModel>();

        UpdateSteelFrameViewModels();

    }

 

    private void UpdateSteelFrameViewModels()

    {

        SteelFrameViewModels.Add(new SteelFrameViewModel()

        {

            Description = WindowFrameTypes.TopHung.Description,

            ProductName = WindowFrameTypes.TopHung.ProductName,

            Value = WindowFrameTypes.TopHung.Value

        });

    }

}

Model.cs

public class SteelFrameViewModel

{

    public string Description { get; set; }

    public string ProductName { get; set; }

    public int Value { get; set; }

}

Kindly provide us with the following information to assist us in providing a better solution as soon as possible:

  1. How do you set the values for SmartEnum properties in your ViewModel, specifically, how do you update the values for the ObservableCollection<SteelFrameViewModel> property?
  2. Can you share the code snippets for the SteelFrameViewModel codes?

We have also attached the sample for your review, and if it is possible to modify the attached sample, it will be helpful in investigating the reported issue.


Attachment: SfDataGridDemo_8d06db6a.zip


TO tonyb February 10, 2023 03:06 PM UTC

Thanks Sathiyathanam,



All I have is a SteelframeModel which I have attached, then from the view model I just load the properties through the models Constructors.


Then is the view Model I have a Observable Collection of the models


Public ObservableCollection<SteelFrameViewModel> _steelWindows = new();


// this will be loaded from a datasource

private void SampleData()

{

    var t = new SteelFrameViewModel(

                ObjectId.NewObjectId(),

                _contract,

                1000,1000,

                WindowFrameTypes.FixedLight,

                10,

                "1A",

                "65f",

                WindowFrameSectionType.W7,

                WindowFrameSectionType.W7,

                WindowFrameSectionType.W7,

                WindowFrameSectionType.W7);


    SteelWindows.Add(t);

}


 when binding the enum to a combobox works fine

 <ComboBox Grid.Row="2"

          Grid.Column="0"

          Margin="{StaticResource XSmallLeftTopRightBottomMargin}"

          HorizontalAlignment="Stretch"

          DisplayMemberPath="Description"

          Header="Frame Type:"

          ItemsSource="{x:Bind ViewModel.FrameTypes}"

          PlaceholderText="Choose Frame type"

          SelectedItem="{x:Bind ViewModel.SelectedFrameType, Mode=TwoWay}"

          ToolTipService.ToolTip="Choose the frame type from the list" />


 when i bind the to Windows community datagrid it works fine see the screen shot i added a SfDatagid and the comunity one


 <StackPanel Grid.Row="2" Margin="{StaticResource XSmallLeftTopRightBottomMargin}">

    <TextBlock Style="{StaticResource SubtitleTextBlockStyle}" Text="Frames" />

    <!--<controls:DataGrid AutoGenerateColumns="True" ItemsSource="{x:Bind ViewModel.SteelWindows, Mode=OneWay}" AreRowGroupHeadersFrozen="True"/>-->


    <TextBlock Text="Syncfusion" />

    <sf:SfDataGrid AutoGenerateColumns="True" ItemsSource="{x:Bind ViewModel.SteelWindows, Mode=OneWay}" />




    <TextBlock Margin="{StaticResource LargeTopMargin}" Text="Community" />

    <controls:DataGrid AutoGenerateColumns="True" ItemsSource="{x:Bind ViewModel.SteelWindows, Mode=OneWay}" />


</StackPanel>


 I uploaded a screenshot to explained


 Regards Tony




Attachment: Samples_8ca5b774.zip


DM Dhanasekar Mohanraj Syncfusion Team February 13, 2023 02:59 PM UTC

Tonyb,


Currently, we are checking the reported scenario with the provided details. we will update you with further details on Feb 15, 2023.


We appreciate your patience until then.



SJ Sathiyathanam Jeyakumar Syncfusion Team February 15, 2023 07:37 AM UTC

Tonyb,

To display properties of custom types, such as the WindowsFrameType enum properties, you can set the SfDataGrid.AutoGenerateColumnsForCustomType property to true. You can also define whether all properties should be displayed or only child properties by setting the SfDataGrid.AutoGenerateColumnsModeForCustomType property to Both or Child respectively. You can refer to the following documentation link to learn more about AutoGenerateColumnsForCustomType and its modes:

<syncfusion:SfDataGrid x:Name="dataGrid"

                        AutoGenerateColumnsForCustomType="True"

                        AutoGenerateColumnsModeForCustomType="Parent"

                        ItemsSource="{x:Bind local:ViewModel.SteelWindows}"

                        AutoGenerateColumns="True">

</syncfusion:SfDataGrid>

UG Link: https://help.syncfusion.com/winui/datagrid/columns#auto-generating-custom-type-property



Attachment: SfDataGridDemo_(2)_61a11503.zip

Marked as answer

TO tonyb replied to Sathiyathanam Jeyakumar February 15, 2023 05:27 PM UTC

Thanks Sathiyathanam adding the two properties 

AutoGenerateColumnsForCustomType="True"

AutoGenerateColumnsModeForCustomType="Parent"

worked .



SJ Sathiyathanam Jeyakumar Syncfusion Team February 16, 2023 06:25 AM UTC

Tonyb,

We are glad that the provided response meets your requirement. Please let us know if you need further assistance. As always, we are happy to help you out.


Loader.
Live Chat Icon For mobile
Up arrow icon