0

I would like to modify the Template of the CellStyle, ElementStyle and EditingElementStyle of the DataGridTextColumn (and also DataGridComboBox and DataGridTemplateColumn later on).

Let's say I have the following column:

<DataGridTextColumn
    CellStyle="{StaticResource DataGridTextColumnCellStyle}"
    ElementStyle="{StaticResource DataGridTextColumnElementStyle}"
    EditingElementStyle="{StaticResource MyPropertyataGridTextColumnEditingElementStyle}"
    Header="My Property">

    <DataGridTextColumn.Binding>

        <Binding
            Mode="TwoWay"
            NotifyOnValidationError="True"
            Path="MyProperty"
            UpdateSourceTrigger="PropertyChanged"
            ValidatesOnDataErrors="True"
            ValidatesOnNotifyDataErrors="True">

            <Binding.ValidationRules>

                <vr:MyPropertyValidationRule ValidationStep="RawProposedValue" />

            </Binding.ValidationRules>

        </Binding>

    </DataGridTextColumn.Binding>

</DataGridTextColumn>

I don't really know what is the difference btw. CellStyle and ElementStyle, but I would rather consider the EditingStyle here. Note that I've prefixed the the key name with MyProperty because I don't know yet how to "reuse" the binding of the DataGridTextColumn inside the Template of the EditingElementStyle.

<Style x:Key="MyPropertyDataGridTextColumnEditingElementStyle" TargetType="{x:Type TextBox}">

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>

                <!--  CustomTextBox is a UserControl that contains some styling, in particular with the validation error  -->
                <!-- (probably a bit overkill, but I have similar other UserControl for ComboBox, DatePicker, etc.  -->
                <controls:CustomTextBox Color="{TemplateBinding Foreground}">

                    <!--  How to avoid rewriting all the binding here are "reuse" the one written in the DataGridTextColumn?  -->
                    <!--  Otherwise, I'm afraid that I will have lots of dupplicated styles for each column  -->
                    <Binding
                        Mode="TwoWay"
                        NotifyOnValidationError="True"
                        Path="MyProperty"
                        UpdateSourceTrigger="PropertyChanged"
                        ValidatesOnDataErrors="True"
                        ValidatesOnNotifyDataErrors="True">

                        <Binding.ValidationRules>

                            <vr:MyPropertyValidationRule ValidationStep="RawProposedValue" />

                        </Binding.ValidationRules>

                    </Binding>

                </controls:CustomTextBox2>

            </ControlTemplate>
        </Setter.Value>
    </Setter>

</Style>

I tried to directly bind the Text property of the CustomTextBox with RelativeSource to the DataGridColumn, but I get errors such as "A two-way binding requires a Path or XPath".

I'm sure that I'm missing something.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.