繁体   English   中英

绑定到自定义控件的嵌套依赖项属性

[英]Binding to nested dependency properties of custom control

我有几个自DockPanel,ToggleButton,ComboBox等派生的自定义控件。我有一个类Props ,我想在每个派生类中用作依赖项属性。 所有这些类都需要具有相同的依赖项属性(包含在Props ),并且可能具有它们自己的几个独特属性(例如,仅在Dock Panel中)。 一个示例用例是属性ExistsInConfig和RightVisible。 我希望控件仅在两者都设置为true时才可见。 我所有自定义派生的控件中都应使用此逻辑。

DockPanel.cs:

public class DockPanel : System.Windows.Controls.DockPanel
{
    public DockPanel()
    {
        Props = new Props();
    }

    public Props Props
    {
        get
        {
            return (Props)GetValue(Properties);
        }
        set
        {
            SetValue(Properties, value);
        }
    }

    public static readonly DependencyProperty Properties =
    DependencyProperty.Register("Props", typeof(Props), typeof(DockPanel), new PropertyMetadata(null));
}

Props.cs:

public class Props: DependencyObject
{
    public Props(){}

    public bool RightVisible { get; set;}

    public bool ExistsInConfig { get; set; }

    public static readonly DependencyProperty RightVisibleProperty =
    DependencyProperty.Register("RightVisible", typeof(bool), typeof(Props), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public static readonly DependencyProperty ExistsInConfigProperty =
    DependencyProperty.Register("ExistsInConfig", typeof(bool), typeof(Props), new FrameworkPropertyMetadata(true, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));

    public static bool GetExistsInConfig(DependencyObject obj)
    {
        return (bool)obj.GetValue(ExistsInConfigProperty);
    }

    public static void SetExistsInConfig(DependencyObject obj, bool value)
    {
       obj.SetValue(ExistsInConfigProperty, value);
    }

    public static bool GetRightVisible(DependencyObject obj)
    {
        return (bool)obj.GetValue(RightVisibleProperty);
    }

    public static void SetRightVisible(DependencyObject obj, bool value)
    {
        obj.SetValue(RightVisibleProperty, value);
    }
}

DockPanel的样式:

<Style x:Key="CustomDockPanelStyle" TargetType="custom:DockPanel">
    <Setter Property="Visibility">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource MultipleBooleanToVisibilityConverter}">
                <Binding RelativeSource="{RelativeSource Self}" Path="Props.ExistsInConfig" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
                <Binding RelativeSource="{RelativeSource Self}" Path="Props.RightVisible" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

在XAML中使用控件:

<Window.Resources>
    <Style TargetType="{x:Type custom:DockPanel}" BasedOn={StaticResource CustomDockPanelStyle}"
</Window.Resources>

<custom:DockPanel 
custom:Props.ExistsInConfig="{Binding ElementName=ToggleCombinedVisibility, Path=IsChecked, Mode=OneWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" 
custom:Props.RightVisible="{Binding ElementName=ToggleCombinedVisibility, Path=IsChecked, Mode=OneWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}">

<ToggleButton x:Name="ToggleCombinedVisibility" IsChecked="True" />

问题是,绑定不起作用。 MultipleBooleanToVisibilityConverter只会在加载视图时调用,而当我尝试使用按钮切换可见性时不会调用。 如果我在RightVisibleExistsInConfig PropertyMetadata中指定回调,则在切换按钮后会调用它们,但转换器不会。

我是否必须让DockPanel知道Props已更改? 我该怎么办? 我找不到在两个类中实现INotifyPropertyChanged的方法。

您的Style中的绑定路径错误。

附加属性应使用括号括起来,而自定义附加属性应在名称空间别名之前。

这应该工作:

<Style x:Key="CustomDockPanelStyle" TargetType="custom:DockPanel">
    <Setter Property="Visibility">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource MultipleBooleanToVisibilityConverter}">
                <Binding RelativeSource="{RelativeSource Self}" Path="(custom:Props.ExistsInConfig)" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged"/>
                <Binding RelativeSource="{RelativeSource Self}" Path="(custom:Props.RightVisible)" NotifyOnSourceUpdated="True" NotifyOnTargetUpdated="True" UpdateSourceTrigger="PropertyChanged" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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