2013-06-12 45 views
0

我有這個WPF是通過表單重複:綁定自定義附加屬性的代碼或樣式/模板

<WrapPanel local:RadioChecker.IsChecked="False"> 
    <RadioButton Content="Yes" Height="16" GroupName = "rbgSeizure" Name="rbSeizureYes" 
        local:RadioChecker.IsChecked="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" /> 
    <RadioButton Content="No" Height="16" GroupName = "rbgSeizure" Name="rbSeizureNo" 
        local:RadioChecker.IsChecked="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" /> 
</WrapPanel> 

什麼是去在同一設置每個WrapPanel如上每個單選按鈕的方式而不必將上面的local:...複製並粘貼到每個控件上?

我可以在代碼中的每個類型的控制背後,如:

GetLogicalChildCollection<RadioButton>(mainPanel) 
    .ForEach(rb => 
    { 
     Binding binding = new Binding(); 
     //binding.Source 
    }); 

但我不知道如何設置綁定的/附加屬性附加到自定義的控件。

+0

一種方式是從單選按鈕獲得MyRadioButtonWithCustomAttachedProperty。 –

回答

1

應該是這樣的:

GetLogicalChildCollection<RadioButton>(mainPanel).ForEach(
    rb => 
    { 
     var binding = new Binding 
     { 
      Path = new PropertyPath(RadioButton.IsCheckedProperty), 
      Source = rb 
     };    
     rb.SetBinding(RadioChecker.IsCheckedProperty, binding); 
    }); 

或本:

GetLogicalChildCollection<RadioButton>(mainPanel).ForEach(
    rb => rb.SetBinding(RadioChecker.IsCheckedProperty, 
         new Binding("IsChecked") { Source = rb })); 
+0

他們都工作,謝謝! –

相關問題