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

Cell showing object type when scrolling and using CellStyleSelector

1 Answer 51 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Maximilian
Top achievements
Rank 1
Maximilian asked on 16 Jul 2013, 01:53 PM
Hello,
I have the following issue: I have data models that keep their state in a property 'ObjectState' that is of type enum. I show a list of these data model objects in a GridView. The first column shows the ObjectState-property. To have a better appearance of this column, I use a StyleSelector to get the Style based on the ObjectState.
The Problem is, that if i scroll horizontally and the column with the style selector gets out of the view, the following column suddenly shows the type of the object instead of the bound value. If I sort the grid, then the bound values appear and everything is fine.
We use version Q3 2012.
Can you help me?

public enum ObjectState
{
    Unchanged = 0,
    New = 1,
    Updated = 2,
    Deleted = 3,
}



public class ObjectStateStyleSelector : StyleSelector
    {
        public Style UnchangedStyle { get; set; }
        public Style ChangedStyle { get; set; }
        public Style AddedStyle { get; set; }
        public Style DeletedStyle { get; set; }
 
        public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container)
        {
            if (item != null)
            {
                Model model = item as Model;
 
                if (model != null)
                {
                    switch (model.ObjectState)
                    {
                        case ObjectState.New:
                            return this.AddedStyle;
 
                        case ObjectState.Updated:
                            return this.ChangedStyle;
 
                        case ObjectState.Unchanged:
                            return this.UnchangedStyle;
 
                        case ObjectState.Deleted:
                            return this.DeletedStyle;
                    }
                }
            }
            return null;
        }
    }



public class Model : INotifyPropertyChanged
    {
        private ObjectState m_objectState;
        private int m_id;
        private string m_description;
         
        public ObjectState ObjectState
        {
            get { return m_objectState; }
            set
            {
                if (m_objectState == value)
                    return;
 
                m_objectState = value;
                OnNotifyPropertyChanged("ObjectState");
            }
        }
 
        public int ID
        {
            get { return this.m_id; }
            set
            {
                if (this.m_id == value)
                    return;
 
                this.m_id = value;
                UpdateState();
                OnNotifyPropertyChanged("ID");
            }
        }
 
        public string Description
        {
            get { return this.m_description; }
            set
            {
                if (this.m_description == value)
                    return;
 
                this.m_description = value;
                UpdateState();
                OnNotifyPropertyChanged("Description");
            }
        }
 
        public Model()
        {
            this.ObjectState = GridProblem.ObjectState.New;
        }
         
        #region INotifyPropertyChanged Members
 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnNotifyPropertyChanged(string prop)
        {           
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
 
        private void UpdateState()
        {
 
            if (m_objectState == GridProblem.ObjectState.Unchanged)
            {
                m_objectState = GridProblem.ObjectState.Updated;
            }
        }
 
        #endregion
    }




<Window x:Class="GridProblem.MainWindow"
        xmlns:main="clr-namespace:GridProblem"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <main:ObjectStateStyleSelector x:Key="GridColumnObjectStateStyleSelector">
            <main:ObjectStateStyleSelector.AddedStyle>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Text="+" FontSize="20" FontWeight="Bold" Foreground="Green"
                                       Margin="0"
                                       HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </main:ObjectStateStyleSelector.AddedStyle>
            <main:ObjectStateStyleSelector.ChangedStyle>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <TextBlock Text="*" FontSize="20" FontWeight="Bold" Foreground="OrangeRed"
                                       HorizontalAlignment="Center" VerticalAlignment="Center"></TextBlock>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </main:ObjectStateStyleSelector.ChangedStyle>
            <main:ObjectStateStyleSelector.UnchangedStyle>
                <Style TargetType="telerik:GridViewCell">
                    <Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </main:ObjectStateStyleSelector.UnchangedStyle>
        </main:ObjectStateStyleSelector>
    </Window.Resources>
    <Grid>
        <telerik:RadGridView x:Name="mainGrid" ItemsSource="{Binding Path=Items}"
                             CanUserInsertRows="True"
                             ShowInsertRow="True"
                             AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn CellStyleSelector="{StaticResource GridColumnObjectStateStyleSelector}"
                                            DataMemberBinding="{Binding Path=ObjectState}" />
                <telerik:GridViewDataColumn Header="ID" DataMemberBinding="{Binding Path=ID}"></telerik:GridViewDataColumn>
                <telerik:GridViewDataColumn Header="Description" DataMemberBinding="{Binding Path=Description}"></telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</Window>

Thanks
Max

1 Answer, 1 is accepted

Sort by
0
Maximilian
Top achievements
Rank 1
answered on 17 Jul 2013, 01:50 PM
Release Q2 2013 solves this issue.
Tags
GridView
Asked by
Maximilian
Top achievements
Rank 1
Answers by
Maximilian
Top achievements
Rank 1
Share this question
or