2013-05-28 81 views
0

我實現了一個自定義控件,我需要執行一些綁定。自定義控件WPF綁定到父元素

public class SomeControl : Control 
    { 
     internal static DependencyProperty AProperty; 
     internal static DependencyProperty BProperty; 
     internal static DependencyProperty CProperty; 

     static AnimationControl() 
     { 

      DefaultStyleKeyProperty.OverrideMetadata(typeof(AnimationControl), new FrameworkPropertyMetadata(typeof(AnimationControl))); 

      AProperty = DependencyProperty.Register("A", typeof(double), typeof(SomeControl)); 
      BProperty = DependencyProperty.Register("B", typeof(Boolean), typeof(SomeControl)); 
      CProperty = DependencyProperty.Register("C", typeof(String), typeof(SomeControl)); 

     } 

     public double A 
     { 
      get { return (double)this.GetValue(AProperty); } 
      set { SetValue(AProperty, value); } 
     } 

     public Boolean B 
     { 
      get { return (Boolean)this.GetValue(BProperty); } 
      set { SetValue(BProperty, value); } 
     } 

     public String C 
     { 
      get { return (String)this.GetValue(CProperty); } 
      set { SetValue(CProperty, value); } 
     } 

    } 

MainWindow.xaml:

<TextBox TextChanged="speedsBoxChanged" Name="speedsBox" Text="1, 2, 3, 4, 5" Grid.Row="0" Grid.Column="2" Margin="2" /> 
     <Slider Name="slider" Grid.Row="0" Grid.Column="3" Margin="2" Minimum="20" Maximum="750" /> 
    <local:AnimationControl        
            A="{Binding A}" 
            B="{Binding B}" 
            C="{Binding C}" /> 
<CheckBox Name="versionCheckBox" Click="VersionChanged" IsChecked="False" VerticalAlignment="Center"/> 

MainWindow.cs:

public event PropertyChangedEventHandler PropertyChanged; 

     public double A 
     { 
      get { return lri.Speed; } 
      set 
      { 
       lri.Speed = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("A")); 
       } 
      } 
     } 

     public string C 
     { 
      get { return speedsBox.Text; } 
      set 
      { 
       speedsBox.Text = value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("B")); 
       } 
      } 
     } 

     public Boolean B 
     { 
      get { return (Boolean)versionCheckBox.IsChecked; } 
      set 
      { 
       versionCheckBox.IsChecked = (Boolean)value; 
       if (PropertyChanged != null) 
       { 
        PropertyChanged(this, new PropertyChangedEventArgs("C")); 
       } 
      } 
     } 

但沒有什麼工作,爲什麼?我一直在試圖弄清楚這一點,但仍然沒有。基本上,我想更新滑塊值更改動畫速度(lri.Speed),從文本框更新C屬性並從複選框更新B.

+1

在SomeControl B中是布爾值,C是字符串,在MainWindow.cs中B是字符串,C是布爾值。也許你應該給你的變量更有意義的名字。 – LPL

+0

事實上,這是正確的,我改變了purpase的變量名稱,對於錯誤感到抱歉。這不是問題,但:( – user208030

回答

0

如果你定義自己的控制和使用綁定,結合特性應在風格爲您的控制,否則,他們將不起作用

樣品:

<Style TargetType="{x:Type local:AnimationControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:AnimationControl}"> 

      ... set the variables via TemplateBinding.... 

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

看到類似的問題:

Custom Control Dependency Property Binding