Is it possible to bind a DependancyProperty to a DataTrigger without exposing the DP with a get-set-property?

Answered Is it possible to bind a DependancyProperty to a DataTrigger without exposing the DP with a get-set-property?

  • Tuesday, April 17, 2012 1:19 PM
     
      Has Code

    I haven't used xaml very much, so this is a newbie question.

    The DataTrigger is part of a template i put on a ListViewItem, while the DP is registrered on the ListViewItems in my namespace. Is it possible to create a binding to the DP like this, without exposing the DP with a get-set-property?

    public static readonly DependencyProperty MarkingProperty = DependencyProperty.Register("MarkingState", typeof(bool), typeof(ListViewItem), new FrameworkPropertyMetadata());
    

         <DataTrigger Binding ="{Binding Path = MarkingState}" Value="false">
                  <Setter Property="Background" Value="Brown"/>
         </DataTrigger>

All Replies

  • Tuesday, April 17, 2012 2:00 PM
     
     
    I doubt it because the DataTrigger can't get the current value of you Dependency Property with the simple reason that there is nog Get-Property in you DP.

    You can try, but that is my first thought and I'm 99% sure that it won't work.
  • Tuesday, April 17, 2012 2:13 PM
     
     Answered Has Code
    Hi

    1.
    If you work with DataTriggers the Binding Path is searched
    in list of properties the DataContext of the styled element,
    not in the FrameworkElement's properties.
    If you work with Triggers the Path is searched in the
    FrameworkElement's properties.

    2.
    If you bind to some data item that has a Path "MarkingState" the
    datacontext needs to expose a property named "MarkingState"
    This can be a simple CLR-property.
    If it only has a DP MarkingStateProperty the binding won't find its source.
    Btw you only need a DP if the property is the Binding target not the binding source.
    For example:
    <TextBox Text="{Binding Path=OwnerName}"
    Text needs to be a DP here OwnerName doesn't need to be a DP and could be a simple CLR property.

    3.
    If you want full support for your DPs in XAML you should
    comply to the common naming convention, which adds a "Property"
    suffix to the DP name, registers the DP with name exclusive of the
    "Property" suffix and also adds a public getter/setter with registered name.

    Such as:

    public static readonly DependencyProperty MarkingStateProperty = DependencyProperty.Register("MarkingState", typeof(bool), typeof(ListViewItem));
    
    public bool MarkingState
    {
        get { return (bool)GetValue(MarkingStateProperty); }
        set { SetValue(MarkingStateProperty, value); }
    }

    Note that the name you register the DP with, doesn't have sth to do with the way you can access the DP in XAML or Code-behind. Technically you could also register your DPs with unique numbers from "1" to "99" or with GUIDs. It would work.
    The registered name is just a name associated internally to the DP,
    which has to be unique within the namescope of the
    DependencyObjects DPs.
    It's could practice to register the DP with the property name anyhow.

    Chris



  • Tuesday, April 17, 2012 2:45 PM
     
     

    Thanks Chris, that was a good explanation.

    The problem is that I have created a modified listview, which generates it's own ListViewItems due to databinding in runtime, so I can't add that exposing Property since i simply don't know how to tell the ListView component to use my custom ListViewItem instead of the ListViewItem base class. I wanted to get around the problem of having to find that out.

  • Tuesday, April 17, 2012 3:47 PM
     
     

    Hi oscar

    Afaics complicated.
    Usually the ListViewItem is generated by the ItemsContainerGenerator at runtime.
    The type of the item is defined by the ItemsControl itself: The ListBox generates ListBoxItems, a ListView generates ListViewItems a DataGrid returns
    DataGridRows  etc.

    There are quite some ways to change the way the items of the listview are displayed:

    • You can choose your own DataTemplate for the ItemTemplate property, but that that will only affect the content of the ListViewItem.
    • You can also set the style of the ListViwItem by changing the ItemContainerStyle.
    • You can choose a different Panel for hosting the items using the ItemsPanel property.


    If you still want to use your own ListViewItem class rather than styling the
    standard ListViewItem, i think you need to implement your own ListView
    class that derives from the standard ListView but has an override for
    the GetContainerForItemOverride method.
    This is the method that decides what kind of element is used as the item container
    of the ItemsControl.
    Here you would return an instance of your own ListViewItem class
    that exposes additional properties such as MarkingState.

    Alternativly adding the  MarkingState property as an attached property
    could come cheaper.

    Chris


  • Tuesday, April 17, 2012 4:00 PM
     
     Proposed

    Hi,

    please check this post, it will provide you some pointer:

    http://stackoverflow.com/questions/2002189/using-custom-dependency-properties-as-datatrigger-in-wpf


    Regards, http://shwetamannjain.blogspot.com

    • Proposed As Answer by Shweta Jain Tuesday, April 17, 2012 4:00 PM
    •  
  • Wednesday, April 18, 2012 6:52 AM
     
     

    Supose I have to give it some thought. Maybe an attached property would be the easiest way to go.

    Thanks

  • Wednesday, April 18, 2012 2:31 PM
     
     
    I overrided GetContainerForItemOVerride and returned my own custom ModifiedListViewItem, which exposes a DP. Functions great. It solved some other issues for me too, thanks chris!