简体   繁体   中英

dynamic property not working well with DependencyProperties

My requirement is Binding dynamic property to my Value (double) property of Custom control.But it not working as expected. Below is the code :

Simple customcontrol part :

xaml

<Style TargetType="{x:Type local:CustomControl1}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomControl1}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                    <TextBox x:Name="PART_TextBox" Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"/>

                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

c#

public class CustomControl1 : Control
{


    public double? Value
    {
        get { return (double?)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Value.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(double?), typeof(CustomControl1), new PropertyMetadata(null, new PropertyChangedCallback(OnValueChanged), new CoerceValueCallback(CoerceValueChange)));

    private static object CoerceValueChange(DependencyObject d, object baseValue)
    {
        return baseValue;
    }

    private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((CustomControl1)d).OnValueChanged(e);
    }

    private void OnValueChanged(DependencyPropertyChangedEventArgs e)
    {
        if (tb != null && e.NewValue != null)
            tb.Text = e.NewValue.ToString();
        else
            tb.Text = string.Empty;
    }


    static CustomControl1()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
    }
    TextBox tb = null;
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        tb = base.GetTemplateChild("PART_TextBox") as TextBox;
    }

}

here goes the usage :

xaml

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <StackPanel Grid.Column="0">
        <Label Content="Double TextBox without Converter" />
        <Label Content="Double TextBox with Converter" />
        <Label Content="MS TextBox" />
        <Button Content="Button" Click="Button_Click_1"/>
    </StackPanel>
    <StackPanel  Grid.Column="1">
        <cc:CustomControl1 Value="{Binding MyValue}" Height="40" Width="200" Background="Red"/>
        <TextBox Height="30" Text="{Binding MyValue}" />
    </StackPanel>
</Grid>

c#

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private dynamic myValue;

    public dynamic MyValue
    {
        get { return myValue; }
        set { myValue = value; RaisePropertyChanged("MyValue"); }
    }

    public MainWindow()
    {
        InitializeComponent();
        this.myValue = 3;
        this.DataContext = this;
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        this.myValue = 5;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

the coerce's base value is always null,but if i use converter in binding it works as expected. but i want to use like normal property , Please help me on this.

binding works perfectly if i use TextBox,it is not the issue ,, i'm facing issue while i use DP's in CustomControl

Regards, Kumar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM