Bind a property to another property of a custom control

user6732861

I am making a custom control based on a button, and I want to bind the width of the button to a property of the class. I have looked at this, this, and this, but they either aren't what I'm looking for, or don't work.

Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomControl">

<Style TargetType="{x:Type local:MyCustomControl}" BasedOn = "{StaticResource {x:Type Button}}">
    <Setter Property = "Background" Value = "LightSalmon" />
    <Setter Property = "Foreground" Value = "Blue"/>
    <Setter Property = "Height" Value = "50"/>
    <Setter Property = "Width" Value = "{Binding MyCustomControl.TextBinding}"/>
    <Setter Property = "VerticalAlignment" Value = "Top"/>
    <Setter Property = "Margin" Value="10"/>
</Style>

</ResourceDictionary>

MyCustomControl.cs

namespace CustomControl
{
public class MyCustomControl : Button
{
    double m_textBinding = 50;
    public double TextBinding
    {
        get { return m_textBinding; }
        set { m_textBinding = value; }
    }
    static MyCustomControl()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), 
            new FrameworkPropertyMetadata(typeof(MyCustomControl)));
    }
}
}

If need be, I can just use the "setter" function, and specify manually, "Width = value;", but I would prefer to use a binding. Currently the "{Binding MyCustomControl.TextBinding}" isn't working.

Clemens

This should work:

<Setter Property="Width"
        Value="{Binding TextBinding, RelativeSource={RelativeSource Self}}"/>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bind a custom type property to a custom control

From Dev

How to bind a property of control to another control's property

From Dev

How to bind a property of control to another control's property

From Dev

Bind to wpf custom control dependency property for tooltip?

From Dev

How to Bind to Another Control Property in WPF

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

How to bind a property to the property of another class (with no UI Control)?

From Dev

Bind control to escaped property?

From Dev

WPF: Custom control property was already registered by another custom control error

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How to bind custom dependecy property to control's view model?

From Dev

How to Bind an another Control on the Setter Value Property by using a Trigger?

From Dev

How to bind user control property to other property?

From Dev

JavaFX bind not property member to control

From Dev

How to bind control to object property?

From Dev

How to bind control to object property?

From Dev

Is it possible to bind the property of a datacontext to the property of another datacontext

From Dev

Is it possible to bind the property of a datacontext to the property of another datacontext

From Dev

Bind custom object property to BooleanBinding

From Dev

Bind Property of a custom class to View

From Dev

(Windows 10 UWP) How to bind Header property in PivotItem control into custom HeaderTemplate in Pivot control?

From Dev

Binding a collection to a custom control property

From Dev

Binding a collection to a custom control property

From Dev

Custom Control Property from DynamicResource

From Dev

Custom control property from a class

From Dev

Add a custom local property to a control?

From Dev

How to bind data to a control's Visibility property

From Dev

bind child user control to class property

From Dev

How to bind data to a control's Visibility property

Related Related

  1. 1

    Bind a custom type property to a custom control

  2. 2

    How to bind a property of control to another control's property

  3. 3

    How to bind a property of control to another control's property

  4. 4

    Bind to wpf custom control dependency property for tooltip?

  5. 5

    How to Bind to Another Control Property in WPF

  6. 6

    How to bind a property to the property of another class (with no UI Control)?

  7. 7

    How to bind a property to the property of another class (with no UI Control)?

  8. 8

    Bind control to escaped property?

  9. 9

    WPF: Custom control property was already registered by another custom control error

  10. 10

    How to bind custom dependecy property to control's view model?

  11. 11

    How to bind custom dependecy property to control's view model?

  12. 12

    How to Bind an another Control on the Setter Value Property by using a Trigger?

  13. 13

    How to bind user control property to other property?

  14. 14

    JavaFX bind not property member to control

  15. 15

    How to bind control to object property?

  16. 16

    How to bind control to object property?

  17. 17

    Is it possible to bind the property of a datacontext to the property of another datacontext

  18. 18

    Is it possible to bind the property of a datacontext to the property of another datacontext

  19. 19

    Bind custom object property to BooleanBinding

  20. 20

    Bind Property of a custom class to View

  21. 21

    (Windows 10 UWP) How to bind Header property in PivotItem control into custom HeaderTemplate in Pivot control?

  22. 22

    Binding a collection to a custom control property

  23. 23

    Binding a collection to a custom control property

  24. 24

    Custom Control Property from DynamicResource

  25. 25

    Custom control property from a class

  26. 26

    Add a custom local property to a control?

  27. 27

    How to bind data to a control's Visibility property

  28. 28

    bind child user control to class property

  29. 29

    How to bind data to a control's Visibility property

HotTag

Archive