Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind trigger to element in parent collection

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?

like image 675
mlemay Avatar asked Jan 16 '23 00:01

mlemay


2 Answers

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.

like image 180
AndrewS Avatar answered Jan 22 '23 06:01

AndrewS


Right now your Trigger is never triggered, as ObservableCollection does not support property changed notifiaction of elements contained.

You could try to implement a specialization of ObservableCollection which supports ChangeNotification as seen here for example Extending ObservableCollection

However, it may be easier to store the first value of your ObservableCollection in your ViewModel/ Code behind and set it as the Trigger's target.

like image 29
Oliver Vogel Avatar answered Jan 22 '23 07:01

Oliver Vogel