mercredi 18 mai 2016

UpdateSourceTrigger don't work in WPF CustomControls

I'm working with WPF and C# on a WPF application in which the controls and their bindings are created at runtime in code-behind. The WPF-window in which i use the controls has a ViewModel with a DataTable as DataContext and - at the bottom - a DataGrid which is bound to the DefaultView of the DataTable.

At first for creating controls at runtime i used the standard WPF-controls, p.e. the TextBox and the CheckBox. In their bindings i set UpdateSourceTrigger to "PropertyChanged" like this:

Binding controlBinding = new Binding();
controlBinding.Source = ViewModelContainer.viewmodel.ApplicationDataSet.Tables[BindingSource].DefaultView;
controlBinding.Path = new PropertyPath("[0][" + BindingPath + "]");
controlBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;

When i changed the text of the TextBox (without leaving it) or checked/unchecked the CheckBox i saw these changes all at once in the DataGrid.

But now i'm using CustomControls which inherit from the standard controls and the UpdateSourceTrigger-functionality doesn't work anymore. When i change the text of the TextBox or check/uncheck the CheckBox i see no changes in the DataGrid.

I think that i have to do something in the definitions of my CustomControls, but what?

Here the definitions of the CustomTextBox and the CustomCheckBox:

<!--Style for the CustomControl CustomTextBox-->
<Style TargetType="{x:Type local:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomTextBox}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox Text="{TemplateBinding Text}"
                             TextWrapping="Wrap"
                             HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                             VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                             ContextMenu="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:CustomTextBox}},
                        Path=ContextMenu}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!--Style for the CustomControl CustomCheckBox-->
<Style TargetType="{x:Type local:CustomCheckBox}" BasedOn="{StaticResource {x:Type CheckBox}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomCheckBox}">
                <Border BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <CheckBox IsChecked="{TemplateBinding IsChecked}"
                              HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                              VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}">
                        <TextBlock Text="{TemplateBinding Text}"
                                   TextWrapping="Wrap"
                                   TextAlignment="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type CheckBox}},
                            Path=HorizontalContentAlignment, Converter={StaticResource h2tAlignmentConverter}}"
                                   TextDecorations="{TemplateBinding TextDecorations}"/>
                    </CheckBox>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire