Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
330
How can one use the EditorStyleSelector
posted

Hi,

I display and edit a bunch of custom objects in the XamDataGrid. For this purpose I have my own Editor and EditorStyles. Here is one example:

    <Style x:Key="PropertyValueStringEditorStyle" TargetType="{x:Type editor:IPropertyValueEditor}">
        <Setter Property="EditTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <Grid HorizontalAlignment="Stretch">
                        <TextBox
                                Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}},
                                Path=Value, Converter={StaticResource pvsc}, Mode=TwoWay}" Height="23">
                        </TextBox>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid HorizontalAlignment="Stretch">
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}},
                                   Path=Value.ToStringProperty}" Height="23"
                                   TextTrimming="CharacterEllipsis">
                        </TextBlock>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

As you can see, the Style has as well Template as EditTemplate Setter.

If I set the Field.Settings.EditorStyle directly, it works! But if I set the Field.Settings.EditorStyleSelector the cells are not editable. Here is the Implementation of the StyleSelector:

        public override System.Windows.Style SelectStyle(object item, System.Windows.DependencyObject container)
        {
            if (container != null)
            {
                var cvp = container as CellValuePresenter;
 
                Type t = cvp.Field.DataType;
 
                if (t == typeof(PropertyValueString))
                {
                    return cvp.TryFindResource("PropertyValueStringEditorStyle"as Style;
                }
                ...            }
            return base.SelectStyle(item, container);
        }
    }

The method returns the right style so the problem is not there, but the CellValuePresenter.IsEditingAllowed remains always False. The Template is set correctly, but the EditTemplate is not (I suppose.)

 

Any help will be appreciated!

 

Regards

George

 

 

 

Parents
  • 34510
    Verified Answer
    Offline posted

    Hi George,

    What is IPropertyValueEditor?  Is that your own custom ValueEditor object?

    I've created a sample using your style and the EditorStyleSelector seems to be able to use the style properly for the column it's applied to.  I'm able to view and edit the data for each cell in that column.  I've attached my sample for your consideration.

    I'm not sure what else you might have in your application but can you download and run my sample to see if it works on your end?

    xamDataGrid-EditorStyleSelector.zip
Reply Children
No Data