自定义控件中的WPF绑定

本文关键字:绑定 WPF 自定义控件 | 更新日期: 2023-09-27 17:50:25

我在WPF中有一个CustomControl .

[TemplatePart(Name = ControlSaveButtonName, Type = typeof(Button))]
[TemplatePart(Name = ControlCancelButtonName, Type = typeof(Button))]
[TemplatePart(Name = ControlPanelName, Type = typeof(Panel))]
public class BaseEditEntityControl : Control
{
    public const string ControlSaveButtonName = "PART_SaveButton";
    public const string ControlCancelButtonName = "PART_CancelButton";
    public const string ControlPanelName = "PART_Panel";
    static BaseEditEntityControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseEditEntityControl), new FrameworkPropertyMetadata(typeof(BaseEditEntityControl)));
    }
    private Button _saveButtonControl;
    public Button SaveButtonControl
    {
        get { return _saveButtonControl; }
    }
    private Button _cancelButtonControl;
    public Button CancelButtonControl
    {
        get { return _cancelButtonControl; }
    }
    private Panel _panelControl;
    public Panel PanelControl
    {
        get { return _panelControl; }
    }
    public static readonly DependencyProperty EntityProperty = DependencyProperty.Register(
        "Entity", typeof(object), typeof(BaseEditEntityControl));
            //, new FrameworkPropertyMetadata
            //{
            //    BindsTwoWayByDefault = true,
            //    PropertyChangedCallback = BaseEntityPropertyChanged
            //});
    public object Entity
    {
        get { return GetValue(EntityProperty); }
        set { SetValue(EntityProperty, value); FillPropertyControls(); }
    }
    public BaseEditEntityControl()
    {
    }
    public override void OnApplyTemplate()
    {
         base.OnApplyTemplate();
        _saveButtonControl = GetTemplateChild(ControlSaveButtonName) as Button;
        _cancelButtonControl = GetTemplateChild(ControlCancelButtonName) as Button;
        _panelControl = GetTemplateChild(ControlPanelName) as Panel;
        if (_saveButtonControl != null) _saveButtonControl.Click += SaveButton_Click;
        if (_cancelButtonControl != null) _cancelButtonControl.Click += CancelButton_Click;
    }
    public void FillPropertyControls()
    { 
        //Some Code
    }
    //private static void BaseEntityPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    //{
    //    var baseEntityControl = d as BaseEditEntityControl;
    //    if (baseEntityControl == null) return;
    //
    //    baseEntityControl.Entity = e.NewValue as BaseEntity;
    //}
}
控制模板:

<Style x:Key="PassportEntityEditControlStyle" TargetType="{x:Type base:BaseEditEntityControl}">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type base:BaseEditEntityControl}">
                <StackPanel x:Name="PART_Panel" Orientation="Vertical">
                    <TextBlock Text="{Binding Entity, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=base:BaseEditEntityControl}}"></TextBlock>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

控制使用:

<Style TargetType="propertyControls1:PersonPassportPropertyControl">
    <Setter Property="IsTabStop" Value="False"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="propertyControls1:PersonPassportPropertyControl">
                <Grid ScrollViewer.CanContentScroll="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                <base:BaseEditEntityControl x:Name="PART_Value" ScrollViewer.CanContentScroll="True"
                      Entity="{Binding PropertyBinding, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=propertyControls1:PersonPassportPropertyControl}}" 
                      Style="{StaticResource PassportEntityEditControlStyle}"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

PersonPassportPropertyControl也是一个自定义控件。

问题:文本框已填满,但未调用'Entity set'和FillPropertyControls。如果取消注释PropertyChangedCallback,则调用BaseEntityPropertyChanged

自定义控件中的WPF绑定

绑定不使用get和set访问器。

按如下方式更改Entity属性:

    //Entity Dependency Property
    public object Entity
    {
        get { return (object)GetValue(EntityProperty); }
        set { SetValue(EntityProperty, value); }
    }
    public static readonly DependencyProperty EntityProperty =
        DependencyProperty.Register("Entity", typeof(object), 
        typeof(BaseEditEntityControl),
        new UIPropertyMetadata(null),
        (d, e) => { FillPropertyControls(); });

这就是它的工作原理。文档中也清楚地提到,XAML绕过属性包装器,直接调用GetValueSetValue

从MSDN:

当前WPF实现的XAML处理器是固有的依赖属性敏感。WPF XAML处理器使用属性系统加载二进制XAML和处理属于依赖属性的属性。这有效绕过属性包装器。当您实现自定义依赖时属性时,必须对这种行为负责,并且应该避免这种行为在属性包装器中放置任何其他代码,而不是

使用 PropertyChangeCallback 来放置你想在依赖属性改变时执行的代码。