2014-09-04 57 views
0

我的要求是将动态属性绑定到自定义控件的Value(double)属性。但它不按预期方式工作。 下面是代码:动态属性与DependencyProperties不能很好地工作

简单customcontrol部分:

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; 
    } 

} 

这里有云的使用:

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)); 
     } 
    } 
} 

的裹胁的基值总是空的,但如果我在结合使用转换器能够正常运行。 但我想使用像普通财产,请帮助我这一点。

结合工程完全如果我使用文本框,它是不是问题,,我现在面临的问题,而我使用DP在CustomControl

问候, 库马尔

+0

@sheridan我试图找到解决办法,但it.Can您分享原帖链接没有得到? – WPFUser 2014-09-04 10:32:47

+0

公认的解决方案只是说'没有问题'绑定'与'动态'属性。因此,你在错误的地方寻找你的错误...在其他地方寻找,也许问一个没有指出问题“动态”属性的新问题。 – Sheridan 2014-09-04 10:41:32

+1

@VenkyDhana,这是一个说英语的论坛。请不要*在这里使用泰米尔语,甚至要问你的朋友,如果他们不明白(或任何* Theriyalaye *的意思)。 – Sheridan 2014-09-04 10:44:31

回答

相关问题