Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8724027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:44:46+00:00 2026-06-13T07:44:46+00:00

I have a control, with a dependency property IsLightOnVal witch is define like this:

  • 0

I have a control, with a dependency property “IsLightOnVal” witch is define like this:

// List of available states for this control
private ObservableCollection<CtlStateBool> m_IsLightOnVal;

[Category("Properties")]
public System.Collections.ObjectModel.ObservableCollection<CtlStateBool> IsLightOnVal
{
    get
    {
        if (m_IsLightOnVal == null)
            m_IsLightOnVal = new System.Collections.ObjectModel.ObservableCollection<CtlStateBool>();
        return m_IsLightOnVal;
    }
    set
    {
        if (m_IsLightOnVal != value)
        {
            m_IsLightOnVal = value;
            OnPropertyChanged("IsLightOnVal");
        }
    }
}

// IsLightOnVal dependency property.
public static readonly DependencyProperty IsLightOnValProperty =
        DependencyProperty.Register("IsLightOnVal", typeof(System.Collections.ObjectModel.ObservableCollection<CtlStateBool>), typeof(ButtonSimple), new UIPropertyMetadata(new System.Collections.ObjectModel.ObservableCollection<CtlStateBool>()));

In my collection, each element contain a string (State) and a bool (Value)

My control’s style is defined in a ControlTemplate.

I want to add a trigger, for example, when the first element in my collection is true, then do something.

I tried this :

<Style x:Key="Btn_RADIO_VHF" TargetType="{x:Type ButtonSimple}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type ButtonSimple}">
            <Canvas .../>
            <ControlTemplate.Triggers>
               <DataTrigger Value="True" Binding="{Binding IsLightOnVal[0].Value, RelativeSource={RelativeSource TemplatedParent}}">
                  <Setter Property="Fill" TargetName="pShowTouch" Value="{DynamicResource ShowTouch}"/>
               </DataTrigger>
            </ControlTemplate.Triggers>

I also tried with a simple Trigger instead of a DataTrigger but it doesn’t seem to support binding…

Can someone help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T07:44:47+00:00Added an answer on June 13, 2026 at 7:44 am

    You’ve got a number of issues there. First, you are using a RelativeSource of TemplatedParent but this isn’t a binding be applied to an element within the template so you should be using Self. So that can be fixed relatively easily:

    <DataTrigger Value="True" Binding="{Binding Path=IsLightOnVal[0].Value, RelativeSource={RelativeSource Self}}">
    

    Second, you have defined this property both as a CLR property (with its own backing store) and as a DependencyProperty. If the property is defined as a DP then the framework is going to expect that you use the DP to store the value. In your code you never use the SetValue method to actually store the collection instance in the DependencyObject’s backing store. So there are a number of ways to fix this:

    1) Remove the DP:

    //public static readonly DependencyProperty IsLightOnValProperty =
    //        DependencyProperty.Register( "IsLightOnVal", typeof( System.Collections.ObjectModel.ObservableCollection<CtlStateBool> ), typeof( ButtonSimple ), new UIPropertyMetadata( new System.Collections.ObjectModel.ObservableCollection<CtlStateBool>() ) );
    

    Since it’s not a DP though you won’t be able to set it in a setter, bind it to some property on your VM, etc. so this probably isn’t the best option.

    2) Store the value in the DP as well as your local variable:

    public System.Collections.ObjectModel.ObservableCollection<CtlStateBool> IsLightOnVal
    {
        get
        {
            if ( m_IsLightOnVal == null )
                this.SetValue(IsLightOnValProperty, m_IsLightOnVal = new System.Collections.ObjectModel.ObservableCollection<CtlStateBool>());
            return m_IsLightOnVal;
        }
        set
        {
            if ( m_IsLightOnVal != value )
            {
                this.SetValue( IsLightOnValProperty, m_IsLightOnVal = value );
                OnPropertyChanged( "IsLightOnVal" );
            }
        }
    }
    

    I personally don’t like this option. Or more specifically I think its bad practice to lazily allocate your own property in the getter. This will set a local value on the object which could overwrite the actual value if someone had actually set it with a lower precedence (e.g. an instance of this was defined in a template and the property set/bound there). And if you plan on design time support this could mess up the designer. If you do go this route then really you should add a PropertyChangedHandler in your DP definition and be sure to set your m_IsLightOnVal member variable in there or else you’ll get out of sync if the value is set through the DP (e.g. someone – including the WPF framework – uses SetValue to set the value of the property).

    3) Only use the GetValue/SetValue

    public System.Collections.ObjectModel.ObservableCollection<CtlStateBool> IsLightOnVal
    {
        get { return (System.Collections.ObjectModel.ObservableCollection<CtlStateBool>)this.GetValue(IsLightOnValProperty); }
        set { this.SetValue( IsLightOnValProperty, value ); }
    }
    

    I would recommend this approach. Yes it means that anyone wishing to set the property must define an instance of the collection but I think this is preferable to the kinds of issues you could hit if you set your own DP value. Note, if you take this route then you may want to define a non-generic collection class that derives from ObservableCollection so that someone could define an instance of the collection class in xaml although if you’re expecting this to just be bound to then this may not be an issue. From the comment on the other reply though it sounds like it may be set in xaml.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control which I would like to add a dependency property
I have a dependency property(List of string) in a user control in my dot
I have a control that look something like this: <asp:DetailsView ID=dvUsers runat=server DataSourceID=odsData DataKeyNames=Id>
I have a wpf control named DataPicker which has a dependency property named SelectedDate.
I have a dependency property on a control which is a custom class. Now
I have a control with a dependency property which I want to pass which
I have a user control that is having issues binding to a dependency property
I have a two dependency properties(both List of strings) in a custom user control.The
I have a rather simple user control (RatingControl) that has a dependency property defined
I have a dependency property (Foreground) on a custom control which is inheriting from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.