UserControl的PropertyChangedCallback无法运行

时间:2019-11-04 12:35:56

标签: c# xaml

我有一个带有某些依赖项属性的用户控件。其中一个(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", typeof(Color), typeof(IndicatorLigth), 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), typeof(IndicatorLigth), new UIPropertyMetadata(true));

        public bool TurnOnValue
        {
            get { return (bool)GetValue(TurnOnValueProperty); }
            set { SetValue(TurnOnValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(bool), typeof(IndicatorLigth),
                                        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, Mode=FindAncestor}, 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, Mode=FindAncestor}, Path=DataContext.Sign}"/>
      <StackPanel>
         <local:IndicatorLigth ActiveColor="Thistle" Value="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.Sign}"/>
      </StackPanel>
    </StackPanel>
  </DataTemplate>
</dxlc:LayoutGroup.HeaderTemplate>
</dxlc:LayoutGroup>

1 个答案:

答案 0 :(得分:0)

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