首页 文章

UpdateSourceTrigger在WPF CustomControls中不起作用

提问于
浏览
1

我在WPF应用程序上使用WPF和C#,其中控件及其绑定是在运行时在代码隐藏中创建的 . 我使用控件的WPF窗口有一个ViewModel,一个DataTable作为DataContext,另一个是DataGrid,它绑定到DataTable的DefaultView .

首先在运行时创建控件我使用标准的WPF控件,例如 . TextBox和CheckBox . 在他们的绑定中,我将UpdateSourceTrigger设置为“PropertyChanged”,如下所示:

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

当我更改TextBox的文本(不离开它)或选中/取消选中CheckBox时,我在DataGrid中同时看到了这些更改 .

但现在我正在使用继承自标准控件的CustomControls,并且UpdateSourceTrigger功能不再起作用 . 当我更改TextBox的文本或选中/取消选中CheckBox时,我看到DataGrid中没有任何更改 .

我认为我必须在CustomControls的定义中做一些事情,但是什么呢?

这里是CustomTextBox和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>

提前致谢!

1 回答

  • 0

    谢谢你!我试过了,这就是我想要的 . 我不得不猜测,我只使用了CustomControls三周而且没有多少经验 .

    你试过 <TextBox Text={Binding Text, RelativeSource={RelativeSource TemplatedParent}, UpdateSourceTrigger=PropertyChanged} ......? - 比迪

相关问题