2014-09-04 55 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

回答

相關問題