代码之家  ›  专栏  ›  技术社区  ›  Femaref

附加的从属属性在所有方向上都不具有约束力

  •  0
  • Femaref  · 技术社区  · 14 年前

        //TimeframeSelector.xaml.cs
        public static readonly DependencyProperty EnableEndFilterProperty =
            DependencyProperty.RegisterAttached(
            "EnableEndFilter", 
            typeof(bool), 
            typeof(TimeframeSelector), 
            new FrameworkPropertyMetadata(false, 
                FrameworkPropertyMetadataOptions.Inherits |
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    
        public static bool GetEnableEndFilter(DependencyObject obj)
        {
            return (bool)obj.GetValue(EnableEndFilterProperty);
        }
    
        public static void SetEnableEndFilter(DependencyObject obj, bool value)
        {
            obj.SetValue(EnableEndFilterProperty, value);
        }
    
        public bool EnableEndFilter
        {
            get { return (bool)GetValue(EnableEndFilterProperty); }
            set { SetValue(EnableEndFilterProperty, value); }
        } 
    

    这些附加的dp用于该控件的xaml中:

    //TimeframeSelector.xaml
    <UserControl x:Class="EveTrader.Wpf.Controls.TimeframeSelector"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:ctrl="clr-namespace:EveTrader.Wpf.Controls"
                 mc:Ignorable="d">
    
                <CheckBox IsChecked="{Binding 
                 Path=(ctrl:TimeframeSelector.EnableEndFilter), 
                 RelativeSource={RelativeSource Self}, Mode=TwoWay}"/>
    </UserControl>
    

    现在,该控件用于另一个控件:

    <ctrl:TimeframeSelector EnableEndFilter="{Binding ApplyEndFilter}" [...]/>
    

    所以问题是:如何让控件双向工作?有些东西已经开始工作了,否则控件将是空的(就像在我将Inherits标志添加到DP之前一样)。

    2 回复  |  直到 14 年前
        1
  •  1
  •   John Bowen    14 年前

    继承的值与绑定不同。当为从父级继承的属性设置值时(就像双向绑定写入源时所做的那样),它会覆盖继承的值。继承权是11项中的第10项 possible value sources .

    在这种情况下,使用带有继承的附加属性并不能获得任何好处,因此可以通过使用标准DP来简化XAML。

    public static readonly DependencyProperty EnableEndFilterProperty =
        DependencyProperty.Register(
        "EnableEndFilter", 
        typeof(bool), 
        typeof(TimeframeSelector), 
        new FrameworkPropertyMetadata(false, 
            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
    

    您可以使用ElementName或RelativeSource绑定将父UserControl直接用作绑定源,这将允许绑定按您希望的方式在两个方向上工作。

    <CheckBox IsChecked="{Binding Path=EnableEndFilter, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ctrl:TimeFrameSelector}}}"/>
    

    默认情况下IsChecked是twoway,因此您也可以从绑定中忽略它。

        2
  •  0
  •   ASanch    14 年前

    尝试将UserControl的XAML更改为:

    <UserControl x:Name="uc" ...>
        <CheckBox IsChecked="{Binding ElementName=uc, Path=(ctrl:TimeFrameSelector.EnableEndFilter), Mode=TwoWay}"/>
    </UserControl>