How to add DependencyProperty to a button?

I'm just trying to add a couple of properties to a button to store additional information that will be used later. My choice to do this instead of creating a button-based UserControl was solely because it seemed smaller than the code.

I followed the examples that I saw on the Microsoft website and I get the error message “A Binding” could not be set in the “SetSortIndicatorVisibility” property of the type 'Button'. set to the DependencyProperty of the DependencyObject. “It doesn't make sense because Button is a DependencyObject, and I add DependencyProperty. I started with AttachedProperty first, but since then I fixed it. I even took all my code that relates to this and put it in a test project, and I still get this error, all this test code is below:

DependencyProperty dependency definition:

public static readonly DependencyProperty SortIndicatorVisibilityProperty = DependencyProperty.Register( "SortIndicatorVisibility", typeof( Visibility ), typeof( Button ), new FrameworkPropertyMetadata( Visibility.Visible, FrameworkPropertyMetadataOptions.AffectsRender ) );

    public static void SetSortIndicatorVisibility( Button button, Visibility value )
    {
        button.SetValue( SortIndicatorVisibilityProperty, value );
    }
    public static Visibility GetSortIndicatorVisibility( Button button )
    {
        return ( Visibility ) button.GetValue( SortIndicatorVisibilityProperty );
    }

XAML window containing a button with a new property and binding:

<Window x:Class="Testing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ext="clr-namespace:Testing"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Test"
            Command="{Binding TestCommand}"
            ext:Class1.SortIndicatorVisibility="{Binding SortIndicatorVisibilitySiteName}" />
</Grid>

Finally, the DependencyProperty property is associated with:

public Visibility SortIndicatorVisibilitySiteName
    {
        get
        {
            return Visibility.Visible;
        }
    }
+5
2

"" , . , Button, ,

+5

, Dependency , getter setter .

( "RegisterAttached" "Register" ).

0

All Articles