UserControl的PropertyChangedCallback无法运行

我有一个带有某些依赖项属性的用户控件。其中一个(ValueProperty)具有PropertyChangedCallback,但从未运行。

namespace test
{
public partial class IndicatorLigth : UserControl
    {
        public IndicatorLigth()
        {
            InitializeComponent();
            DataContext = this;
            CurrentBrush = new SolidColorBrush(InactiveColor);
            lIndicator.Background = CurrentBrush;
            TurnOnValue = true;
            Value = true;
        }       

        public static readonly DependencyProperty activeColorProperty =
            DependencyProperty.Register("activeColor",typeof(Color),typeof(IndicatorLigth),new UIPropertyMetadata(Colors.Green));

        public Color activeColor
        {
            get { return (Color)Getvalue(activeColorProperty); }
            set { Setvalue(activeColorProperty,value); }
        }

        public static readonly DependencyProperty InactiveColorProperty =
            DependencyProperty.Register("InactiveColor",new UIPropertyMetadata(Colors.Red));

        public Color InactiveColor
        {
            get { return (Color)Getvalue(InactiveColorProperty); }
            set { Setvalue(InactiveColorProperty,value); }
        }

        private SolidColorBrush _currentBrush;

        public SolidColorBrush CurrentBrush
        {
            get { return _currentBrush; }
            set { _currentBrush = value; }
        }

        public static readonly DependencyProperty TurnOnValueProperty =
            DependencyProperty.Register("TurnOnValue",typeof(bool),new UIPropertyMetadata(true));

        public bool TurnOnValue
        {
            get { return (bool)Getvalue(TurnOnValueProperty); }
            set { Setvalue(TurnOnValueProperty,value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value",new FrameworkPropertyMetadata(true,FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,OnSetColorChanged));

        public bool Value
        {
            get { return (bool)Getvalue(ValueProperty); }
            set
            {
                Setvalue(ValueProperty,value);                
            }
        }

        private void CheckStatus(bool sign)
        {            
            if (sign == TurnOnValue)
                CurrentBrush = new SolidColorBrush(activeColor);
            else CurrentBrush = new SolidColorBrush(InactiveColor);  
        }

        private static void OnSetColorChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)
        {
            IndicatorLigth mycontrol = d as IndicatorLigth;
            mycontrol.callmyInstanceMethod(e);
        }
        private void callmyInstanceMethod(DependencyPropertyChangedEventArgs e)
        {
            CheckStatus((bool)e.NewValue);
            lIndicator.Background = CurrentBrush;
        }
    }
}

在使用我的用户控件的XAML(我在另一个UserControl中使用它):

<UserControl
             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:local="clr-namespace:test"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
...
<StackPanel Orientation="Vertical">
   <Label Content="{Binding RelativeSource={RelativeSource AncestorType=UserControl,Mode=FindAncestor},Path=DataContext.Sign}"/>
   <StackPanel>
       <local:IndicatorLigth activeColor="Thistle" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl,Path=DataContext.Sign}"/>
   </StackPanel>
</StackPanel>

Sign参数属于ComboBox的IsEnabled可绑定属性,该属性不在XAML代码中。标签内容正确,当我更改组合框启用状态时它会更改,但是我的UserControl值设置器,OnSetColorChanged和callmyInstanceMethod不会触发。你能告诉我我的代码有什么问题吗?非常感谢。

更新:所以我错了。上面提到的代码是正确的。当我将堆栈面板推入devexpress LayoutGroup HeaderTemplate时,将发生问题:

<dxlc:LayoutGroup Orientation="Vertical" VerticalAlignment="Top"> 
<dxlc:LayoutGroup.HeaderTemplate>
  <DataTemplate>
    <StackPanel Orientation="Vertical">
      <Label Content="{Binding RelativeSource={RelativeSource AncestorType=UserControl,Path=DataContext.Sign}"/>
      <StackPanel>
         <local:IndicatorLigth activeColor="Thistle" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl,Path=DataContext.Sign}"/>
      </StackPanel>
    </StackPanel>
  </DataTemplate>
</dxlc:LayoutGroup.HeaderTemplate>
</dxlc:LayoutGroup>
bluecapucino 回答:UserControl的PropertyChangedCallback无法运行

很抱歉打扰您,并感谢您的建议。我已经找到问题的原因。我不需要使用<HeaderTemplate><DataTemplate>,我必须使用简单的<Header>块:)

本文链接:https://www.f2er.com/3166729.html

大家都在问