Invoking a custom dependency property defined in XAML code

Is it possible to call a custom dependency property in the XAML of the element in which it is defined?

I mean, I have the following simple code for my mainWindow:

the code

public partial class MainWindow : Window
{

    public static readonly DependencyProperty SpecialToProperty =   DependencyProperty.Register("SpecialTo", typeof(double), typeof(MainWindow));

    public MainWindow()
    {

        InitializeComponent();

    }

    public double SpecialTo
    {
        get
        {
            return (double)GetValue(SpecialToProperty);
        }
        set
        {
            SetValue(DoubleAnimation.ToProperty, value);
        }
    }
}

How can I use this dependency property from the partial XAML code of the MainWindow class?

I mean something like:

<Window x:Class="WpfAnimationTEst.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"        
    SpecialTo=200>

I know this can be done using related dependency properties, but is this the only way? Is it impossible to call the dependency property defined in the code?

Thank you and sorry if the question is kind of stupid, I'm just studying and trying to understand WPF.

+4
source share
3 answers

, :
, andreask . BaseClass :
1) :

public class BaseWindow : Window {
    public BaseWindow() { }

    public static readonly DependencyProperty SpecialToProperty = DependencyProperty.Register("SpecialTo", typeof(double), typeof(BaseWindow));

    public double SpecialTo {
        get {
            return (double)GetValue(SpecialToProperty);
        }
        set {
            SetValue(SpecialToProperty, value);
        }
    }
}

.
2) MainWindow xaml:
( YOURNAMESPACE (2x) )

<local:BaseWindow x:Class="YOURNAMESPACE.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:YOURNAMESPACE"
        Title="MainWindow" Height="350" Width="525" SpecialTo="100">
    <Grid>

    </Grid>
</local:BaseWindow>

3) MainWindow.cs:

 public partial class MainWindow : BaseWindow {
        public MainWindow() {
            InitializeComponent();
        }
    }

, xaml .

+2

XAML, XAML. , TextBox:

public class MyTextBox : TextBox
{
    public static readonly DependencyProperty SpecialToProperty = DependencyProperty.Register("SpecialTo", typeof(double), typeof(MyTextBox));

    public double SpecialTo
    {
        get
        {
            return (double)GetValue(SpecialToProperty);
        }
        set
        {
            SetValue(DoubleAnimation.ToProperty, value);
        }
    }
}

, , MyTextBox XAML SpecialTo:

<custom:MyTextBox SpecialTo="1.0" />

, , MainWindow, Window, Window (SpecialTo Window, MainWindow).

, MainWindow :

<custom:MainWindow
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"        
    SpecialTo=200>

, x:class, XAML codebehind ( ), , ...

0

, , , . , , , .

, . , , , .

: , , , "" XAML. , .

For me, the best jobs for using the custom dependency property in XAML defined in the code are those sent by @Clemens and @Noel Widmer. This and this

0
source

All Articles