Search code examples
c#wpfxamldevexpress-wpf

Devexpress wpf CellEditor bind to class instance


I have dxg:GridControl.
xaml:

        <dxg:GridControl Name="DynamicGridControl"
                         ItemsSource="{Binding CommonEditCollection, Mode=TwoWay}"
                         SelectionMode="Cell"
                         AutoGenerateColumns="AddNew"
                         AutoGeneratedColumns="GridControl_AutoGeneratedColumns">
            <dxmvvm:Interaction.Behaviors>
                <lc:CellSelectionBehavior SelectedCells="{Binding SelectedCells, Mode=TwoWay}"/>
            </dxmvvm:Interaction.Behaviors>
        </dxg:GridControl>

ItemsSource binds to CommonEditCollection
viewmodel:

public ObservableCollection<Dictionary<int, DynamicTableModel>> CommonEditCollection { get; set; }

model:

 public class DynamicTableModel
    {
        public double CellWidth { get; set; }
        public string StrValue{ get; set; }
        public bool IsBorerNull { get; set; }

        public DynamicTableModel(string strVal, double cellWidth, bool isBorerNull = false)
        {
            StrValue = strVal;
            CellWidth = cellWidth;
            IsBorerNull = isBorerNull;
        }
    }

In xaml file I set Resources for cells style (I want to merge some cells):

            <DataTemplate x:Key="CellDataTemplate">
                <StackPanel>
                    <Border ...
                        </Border.Style>
                    </Border>
                    <dxg:CellEditor Content="{Binding Value.StrValue}"/>
                </StackPanel>
            </DataTemplate>

I bind CellEditor to property of DynamicTableModel class. But if I try edit text in any cell it throw NullReferenceException.
I cant change class DynamicTableModel to string because I need other properties. And I tried to use convertor Attribute, but it create new instance when I change text.
Help me please to change text in cells. Project link: https://github.com/Kolgotin/DynamicGridControl


Solution

  • In result I just added this property:

    <DataTemplate>
        <dxe:TextEdit Name="PART_Editor" HorizontalContentAlignment="Stretch">
            <dxe:TextEdit.EditTemplate>
                <ControlTemplate>
                    <dxe:TextEdit x:Name="teNewValue"
                                HorizontalAlignment="Stretch"
                                EditValue="{Binding Value.StrValue}"/>
                </ControlTemplate>
            </dxe:TextEdit.EditTemplate>
        </dxe:TextEdit>
    </DataTemplate>
    

    and handler works in "set" method