This is a migrated thread and some comments may be shown as answers.

Binding column to enum with integer values

4 Answers 410 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Bryce
Top achievements
Rank 1
Bryce asked on 12 Apr 2012, 07:24 PM
Hi,

I have a project that has an enum, and my enum looks like this:

public enum Color
{
    Red= 2,
 
    Green= 3,
 
    Blue= 1
}

These aren't the exact values, but the data we are using grabs information and the integers represent that data, so we are basically given that order (note: this is just an example of an enum that has a similar situation to ours). If I bind a column to the enum, the column is sorted by the integer value, not the value on the left. This affects sorting cause now the sort will not sort correctly (it will be blue red green rather than blue green red). Is there a way around this?

Thanks,
Bryce

4 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 17 Apr 2012, 07:10 AM
Hi,

 To achieve your goal, you can use a generic SortDescriptor. For example if your GridView is "PlayersGrid", then the code would look like:

private void Button_Click(object sender, RoutedEventArgs e)
       {
           var desc1 = new SortDescriptor<Player, string>();
           desc1.SortingExpression = item => item.Color.ToString();
           desc1.SortDirection = ListSortDirection.Descending;
           this.playersGrid.SortDescriptors.Add(desc1);
       }

I hope that this helps.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Bryce
Top achievements
Rank 1
answered on 17 Apr 2012, 10:01 PM
Hi Didie,

I think I am misunderstanding something. This is what I have:

public partial class CasesView : UserControl
    {
        public CasesView()
        {
            InitializeComponent();
            Messenger.Default.Register<double>(this, "changeOpacity", ChangeOpacity);
             
            ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(RadGridView), new GridViewCustomPropertyProvider());
            ServiceProvider.RegisterPersistenceProvider<ICustomPropertyProvider>(typeof(RadDocking), new DockingCustomPropertyProvider());
             
            Application.Current.Exit += (s, e) =>
            {
                IsolatedStorageProvider provider = new IsolatedStorageProvider();
                provider.SaveToStorage();
            };
             
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
 
            this.radGridView.DataLoaded += RadGridView1_DataLoaded;
        }
 
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            IsolatedStorageProvider provider = new IsolatedStorageProvider();
            provider.LoadFromStorage("DockingControln5");
        }
             
 
        private void ChangeOpacity(double opacity)
        {
            this.secureXCases.Opacity = opacity;
        }
 
        private void RadDocking_PaneStateChange(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            foreach (RadPane pane in radDocking.Panes)
            {
                if (pane != null && pane.IsFloating)
                {
                    ToolWindow toolWindow = GetToolWindow(pane);
                    toolWindow.Height = 200;
                    toolWindow.Width = 600;
                    toolWindow.DataContext = radDocking.DataContext;
                }
            }
        }
 
        private ToolWindow GetToolWindow(RadPane pane)
        {
            ToolWindow window = pane.ParentOfType<ToolWindow>();
            if (window == null)
            {
                window = (((pane.Parent as RadPaneGroup).Parent as RadSplitContainer).Parent) as ToolWindow;
            }
            return window;
        }
 
        void RadGridView1_DataLoaded(object sender, EventArgs e)
        {
            var casesViewModel = this.DataContext as SecureX5CasesViewModel;
 
            if (casesViewModel != null)
            {
                if (!casesViewModel.IsFirstCaseSelected)
                {
                    if (radGridView.Items.Count > 0)
                    {
                        this.radGridView.SelectedItem = this.radGridView.Items[0];
                    }
                }
            }
        }
 
        private void radGridView_RowLoaded(object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
        {
            if (!(e.Row is GridViewHeaderRow) && !(e.Row is GridViewNewRow))
            {
                RadContextMenu rowContextMenu = new RadContextMenu(); // create menu
                StyleManager.SetTheme(rowContextMenu, new MetroTheme()); // set menu Theme
 
                // create menu items
                rowContextMenu.Items.Add(new RadMenuItem() { Header = "Copy Case Url" });
 
                // add menu events
                rowContextMenu.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClick));
                rowContextMenu.Opened += new RoutedEventHandler(OnMenuOpened);
 
                // attach menu
                RadContextMenu.SetContextMenu(e.Row, rowContextMenu);
            }
        }
 
        void OnMenuOpened(object sender, RoutedEventArgs e)
        {
            GridViewRow row = ((RadRoutedEventArgs)e).OriginalSource as GridViewRow;
            if (row != null)
            {
                row.IsSelected = row.IsCurrent = true;
            }
        }
 
        void OnMenuItemClick(object sender, RoutedEventArgs e)
        {
            RadMenuItem clickedItem = ((RadRoutedEventArgs)e).OriginalSource as RadMenuItem;
            if (clickedItem != null)
            {
                string header = Convert.ToString(clickedItem.Header);
 
                var selectedItem = radGridView.SelectedItem as DisplayCaseViewModel;
                if (selectedItem != null)
                {
                    var url = selectedItem.CaseUrl;
                    HtmlPage.Window.Eval("window.clipboardData.setData('Text','" + url + "')");
                }
            }
        }
 
        bool isGridLoaded;
        private void RadGridView_Loaded(object sender, RoutedEventArgs e)
        {
            if (!isGridLoaded)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    IsolatedStorageProvider provider = new IsolatedStorageProvider();
                    provider.LoadFromStorage("GridView1");
                    isGridLoaded = true;
 
                    var desc1 = new SortDescriptor<DisplayCaseViewModel, string>();
                    desc1.SortingExpression = item => item.Type.ToString();
                    desc1.SortDirection = ListSortDirection.Descending;
                    this.radGridView.SortDescriptors.Add(desc1);
                }));
            }
        }
    }

DisplayCaseViewModel is the row which has the Type property. So when my grid is loaded, I want the sort descriptors to be added. The XAML looks like this for that column:

<telerik:GridViewDataColumn Header="Type"
                                                    SortMemberPath="Type"
                                                    DataMemberBinding="{Binding Type}"
                                                    HeaderCellStyle="{StaticResource GridViewHeaderCellStyle}">
                                                    <telerik:GridViewDataColumn.CellTemplate>
                                                        <DataTemplate>
                                                            <Image Height="20" Width="20" Source="{Binding Type,Converter={StaticResource DocTypeToBitmap}}" >
                                                                <ToolTipService.ToolTip>
                                                                    <StackPanel Orientation="Horizontal">
                                                                        <TextBlock Text="Type: " />
                                                                        <TextBlock Text="{Binding Type}" />
                                                                    </StackPanel>
                                                                </ToolTipService.ToolTip>
                                                            </Image>
                                                        </DataTemplate>
                                                    </telerik:GridViewDataColumn.CellTemplate>
                                                </telerik:GridViewDataColumn>

So that is the column that has the Type with the enum. I'm not sure what is going wrong. Any suggestions?

Thanks,
Bryce

0
Dimitrina
Telerik team
answered on 18 Apr 2012, 01:46 PM
Hello Bryce,

 Such a sorting is expected when it comes to enum type. Please check the MSDN for a reference.

If you do not like to use the generic descriptor, then another way to sort the column would be to specify a valid SortMemberPath property for the column. 

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Bryce
Top achievements
Rank 1
answered on 18 Apr 2012, 06:09 PM
Hi Didie,

I would like to use a generic sort desciptor, but I was just saying for some reason it didn't work and was wondering if I did something wrong and if you could give an example using what I have posted.

Thanks,
Bryce
Tags
GridView
Asked by
Bryce
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Bryce
Top achievements
Rank 1
Share this question
or