简体   繁体   中英

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, pe 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!

Thank You bidy! I tried it and that's what i was looking for. I have to guess, that i'm working with CustomControls since just three weeks and have not much experience.

Have you tried <TextBox Text={Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged} ... ? – bidy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM