C# 自定义指示器控件

C# 自定义指示器控件,c#,wpf,data-binding,dependency-properties,C#,Wpf,Data Binding,Dependency Properties,控制C代码: public partial class RedGreenStatusIndicator : UserControl, INotifyPropertyChanged { public RedGreenStatusIndicator() { this.InitializeComponent(); DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromPr

控制C代码:

public partial class RedGreenStatusIndicator : UserControl, INotifyPropertyChanged
{
    public RedGreenStatusIndicator()
    {
        this.InitializeComponent();

        DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty
            (ArchiverDetails.ArDetailsProperty,
            typeof(ArchiverDetails));

        dpd.AddValueChanged(this, delegate { this.ObjectValueChanged(); });
        Status = false;
    }

    void RedGreenStatusIndicator_Loaded(object sender, RoutedEventArgs e)
    {

    }

    public bool Status
    {
        get { return (bool)GetValue(StatusProperty); }
        set 
        {
            bool old_value = Status;
            SetValue(StatusProperty, value);

            if ((old_value == true) && (Status == false))
            {
                hide_green();
                show_red();
            }

            if((old_value == false) && (Status == true))
            {
                hide_red();
                show_green();
            }
        }
    }

    private void show_green()
    {
        if (GreenInterior.Opacity == 0)
            run_storyboard("show_green_indicator");
    }

    private void hide_green()
    {
        if (GreenInterior.Opacity != 0)
            run_storyboard("hide_green_indicator");
    }

    private void show_red()
    {
        if (RedInterior.Opacity == 0)
            run_storyboard("show_red_indicator");
    }

    private void hide_red()
    {
        if (RedInterior.Opacity != 0)
            run_storyboard("hide_red_indicator");
    }

    private void run_storyboard(string resource_name)
    {
        Storyboard sb = (Storyboard)FindResource(resource_name);
        sb.Begin();
    }

    public static readonly DependencyProperty StatusProperty =
        DependencyProperty.Register("Status",
        typeof(bool),
        typeof(RedGreenStatusIndicator),
        new PropertyMetadata(null));

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    private void ObjectValueChanged()
    {
        OnPropertyChanged("Status");
    }

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion
}
XAML:

以下是我的问题:

我到底做错了什么? 我能否在ListViewItem模板中使用此选项?listview将被数据绑定到一个可观察的对象集合,其中包含我希望绑定到的bool属性


我需要做些什么才能使它工作?

试着将代码保持在尽可能小的范围内。 特别是:不要将任何内容放入依赖项属性access属性的setter中-当WPF更改值时,不会执行此代码

试试这个: 代码隐藏:

public partial class StatusIndicator 
    : UserControl
{
    public static readonly DependencyProperty IsGreenProperty = DependencyProperty.Register("IsGreen", typeof(bool), typeof(StatusIndicator), new UIPropertyMetadata(false));

    public bool IsGreen
    {
        get
        {
            return (bool) GetValue(IsGreenProperty);
        }
        set
        {
            SetValue(IsGreenProperty, value);
        }
    }


    public StatusIndicator()
    {
        InitializeComponent();
    }
}
XAML:


尝试将代码保持在尽可能小的范围内。 特别是:不要将任何内容放入依赖项属性access属性的setter中-当WPF更改值时,不会执行此代码

试试这个: 代码隐藏:

public partial class StatusIndicator 
    : UserControl
{
    public static readonly DependencyProperty IsGreenProperty = DependencyProperty.Register("IsGreen", typeof(bool), typeof(StatusIndicator), new UIPropertyMetadata(false));

    public bool IsGreen
    {
        get
        {
            return (bool) GetValue(IsGreenProperty);
        }
        set
        {
            SetValue(IsGreenProperty, value);
        }
    }


    public StatusIndicator()
    {
        InitializeComponent();
    }
}
XAML:

m_indicator.DataContext = this;
public partial class StatusIndicator 
    : UserControl
{
    public static readonly DependencyProperty IsGreenProperty = DependencyProperty.Register("IsGreen", typeof(bool), typeof(StatusIndicator), new UIPropertyMetadata(false));

    public bool IsGreen
    {
        get
        {
            return (bool) GetValue(IsGreenProperty);
        }
        set
        {
            SetValue(IsGreenProperty, value);
        }
    }


    public StatusIndicator()
    {
        InitializeComponent();
    }
}
<UserControl x:Class="WpfApplication1.StatusIndicator"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication1="clr-namespace:WpfApplication1"
    Height="300" Width="300" x:Name="this">
    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type WpfApplication1:StatusIndicator}">
            <ControlTemplate.Triggers>
                <DataTrigger Binding="{Binding ElementName=this, Path=IsGreen}"
                             Value="True">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard FillBehavior="HoldEnd">
                                <DoubleAnimation Duration="0:0:0.500"
                                                 From="0"
                                                 To="1"
                                                 Storyboard.TargetName="green"
                                                 Storyboard.TargetProperty="Opacity" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>

                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Duration="0:0:0.500"
                                                 From="1"
                                                 To="0"
                                                 Storyboard.TargetName="green"
                                                 Storyboard.TargetProperty="Opacity" />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </ControlTemplate.Triggers>

            <Grid>
                <Rectangle x:Name="red"
                           Fill="Red"/>
                <Rectangle x:Name="green"
                           Fill="Green" 
                           Opacity="0" />
            </Grid>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>