locked
DataGrid sets Property but don't get value to change SelectedIndex RRS feed

  • Question

  • I'm having a weird problem in a Datagrid. I'm using WPF with DataBinding, I have a DataGrid, and 3 buttons in a Page - Save, Update, Cancel - and a lot of labels. These labels, when user clicks the Update Button, will enable editing mode to these labels. 
    While in edit mode, that user can't change SelectedIndex of that Grid (that's my problem). I've tried to create a Property IsNotEditable, Binding to IsEnabled="" of that DataGrid, but if it's disabled, the current row is unselected. 
    I can't use that, because when user clicks Save, i'll save that Selected Row. So... I've created another Property PlacasSelectedItem, and a 'Support' Property PlacasSelectedAux, with this code:
     public ConeSlab PlacasSelectedAux { get; set; }
    
        private ConeSlab placasSelectedItem;
        public ConeSlab PlacasSelectedItem
        {
            get { return placasSelectedItem; }
            set
            {
                if (CurrentEditMode == EditMode.View)
                {
                    placasSelectedItem = value;
                    PlacasSelectedAux = value;
                    OnPropertyChanged("PlacasSelectedItem");
    
                    if (PlacasSelectedItem != null)
                        PlacaQuenteIsChecked = StringUtil.ConvertYesNoToBoolean(PlacasSelectedItem.Slab.InfHotSlab);
                    else
                        PlacaQuenteIsChecked = false;
    
                    ExibeLaminadorDestino();
                }
                else if (CurrentEditMode != EditMode.View)
                {
                    // if isn't in ViewMode, and if user clicks in another Row, will force previously row to be selected.
                    placasSelectedItem = PlacasSelectedAux;
                    OnPropertyChanged("PlacasSelectedItem");
                }
            }
        }
    
    Ok, now the weird problem: It executes the code, I see the change in the code, but the SelectedIndex doesn't change! The Databinding doesn't work!
    Here is my Binding:
    <DataGrid SelectedItem="{Binding Path=PlacasSelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
    
    Basically, what I need is to avoid user changes the selected row while in editing mode.

    Thanks for any help!
    Wednesday, October 19, 2011 6:28 PM

Answers

  • Hi Gustavo Souza Goncalves,

    If you could record the SelectedItem before you disable your DataGrid, and after editting, you could recover this SelectedItem, there is more information you could refer to:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97920757-a0ed-4488-b1a7-2a2b55fb1014

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 8:20 AM
  • Thanks Sheldon_Xiao, but I did as follows:

    Into the xaml's Datagrid:

    IsHitTestVisible="{Binding Path=IsEditing, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBooleanConverter}}" 
    


    Created a Static Resource:

     <Page.Resources>
        <ut:InverseBooleanConverter x:Key="InverseBooleanConverter" />
     <Page.Resources>
    


    This will inverse the Boolean value to disable changing Datagrid row selection, using this method:

    /// <summary>
    /// Convert bool to !bool
    /// </summary>
    [ValueConversion(typeof(Boolean), typeof(Boolean))]
    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            return !(Boolean)value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    Now the user can't select any other row in the DataGrid! Thanks for your help :)


    Att. Gustavo Souza Gonçalves
    Monday, October 24, 2011 10:52 AM

All replies

  • Hi Gustavo Souza Goncalves,

    If you could record the SelectedItem before you disable your DataGrid, and after editting, you could recover this SelectedItem, there is more information you could refer to:

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/97920757-a0ed-4488-b1a7-2a2b55fb1014

     

    Best regards,


    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, October 24, 2011 8:20 AM
  • Thanks Sheldon_Xiao, but I did as follows:

    Into the xaml's Datagrid:

    IsHitTestVisible="{Binding Path=IsEditing, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InverseBooleanConverter}}" 
    


    Created a Static Resource:

     <Page.Resources>
        <ut:InverseBooleanConverter x:Key="InverseBooleanConverter" />
     <Page.Resources>
    


    This will inverse the Boolean value to disable changing Datagrid row selection, using this method:

    /// <summary>
    /// Convert bool to !bool
    /// </summary>
    [ValueConversion(typeof(Boolean), typeof(Boolean))]
    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            return !(Boolean)value;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
            System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    Now the user can't select any other row in the DataGrid! Thanks for your help :)


    Att. Gustavo Souza Gonçalves
    Monday, October 24, 2011 10:52 AM