Binding IsEnable on my UserControl but need it's dependency properties RRS feed

  • General discussion

  • Problem:

    How can I get the values of my UserControl's dependency properties into the Binding to a class object?


    I have a user control that is basically a Button with a StackPanel and in that are an Image and TextBlock

        <Button x:Name="MainButton" Height="40" Margin="2" >
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" MinWidth="146">
                <Image x:Name="ButtonImg" Margin="1" HorizontalAlignment="Left" />
                <TextBlock x:Name="ButtonTxt" Margin="8,2,2,2" VerticalAlignment="Center" />

    I have created two dependency properties for the UserControl that are referenced in XAML:

    <DC:DCButtonMain x:Name="DoItButton" EventType="Forms" EventLinkID="DoItSingle" ButtonImage="../Graphics/DoIt32.png" ButtonText="Do IT!" IsEnabled="{Binding Path=DoItButtonEnable, UpdateSourceTrigger=PropertyChanged, TargetNullValue=False, FallbackValue=False, Mode=OneWay}"/>

    I have a class that handles the UI (not true MVVM but meets my needs for efficiency and performance) and it's DataContext is set.  The DoItButtonEnable has an OnPropertyChanged call so the UI gets updated when DoItButtonEnable value changes in my UI object.  

    The problem is, the business logic that determines the DoItButtonEnable (IsEnable = true/false) needs to know the values of the button's dependency properties -- the EventType and EventLinkID as these are keys to looking up a match (on another object collection) to see if the button should be enabled.

    I can't find a clean way to accomplish this via my UI object, the only approach is code behind either in the UserControl or in the Page using the UserControl.

    Any ideas (other than installing Prism and doing full MVVM)?

    Thursday, August 23, 2012 4:43 PM

All replies

  • Have you created a CLR property to give access to the DependencyProperty? It is quite normal to have code like

    public static readonly DependencyProperty EventTypeProperty = 
                                          DependencyProperty.Register("EventType", .. etc...);
    public string EventType
       get { return GetValue(EventTypeProperty).ToString(); }
       set { SetValue(EventTypeProperty, value);}

    From your business logic you would use the EventType property to get and/or set the dependency property.  It is not a good idea to have any extra logic in the getter/setter apart from what I have shown.

    Thursday, August 23, 2012 7:36 PM
  • The code you listed is Dependecy Property code not a CLR property??  Dependency property is only available in the Partial Class that inherits UserControl ... which is not my case, I have is a standard SL Class that my UI binds to. 

    Friday, August 24, 2012 3:50 PM
  • public string EventType {get {...} set { ...} }

    That's creating a CLR property.



    That's creating a dependency property


    If you don't know exactly where to implement my answer then post the code that you do have and I can show you how to modify it. 

    Friday, August 24, 2012 4:18 PM