2010-09-09 48 views
1

如何以編程方式在代碼中實現這一行XAML? (因爲我需要來動態創建的單選按鈕,但他們怎麼有綁定)如何在WPF代碼中以編程方式實現這一行XAML?

<RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Proxy}">Proxy</RadioButton> 

我也得在這裏(見下文),到目前爲止,但現在我再掙扎如何讓綁定被吸引。我做了一個猜想/假設我應該使用Binding類...

foreach (KeyValuePair<int, string> @interface in interfaces) 
    { 
     // RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Proxy, UpdateSourceTrigger=PropertyChanged , Mode=TwoWay}">Proxy</RadioButton> 

     // Create Radio Button 
     var newRb = new RadioButton(); 
     newRb.Name = "I" + @interface.Key.ToString(); 
     newRb.GroupName = "InterfaceGroup"; 
     newRb.Content = @interface.Value; 

     // Binding 
     var binding = new Binding(); 
     binding.Source = "Interface"; 
     binding.Converter = new RadioBoolToIntConverter(); 
     binding.ConverterParameter = @interface.Key; 

     // STUCK HERE - RE HOW TO GET THE BINDING TO BE APPLIED TO THE RADIO BUTTON 

     InterfacesRadioButtons.Children.Add(newRb); 
    } 

併爲背景的依賴對象的模型類:

public class ConfigWindowViewModel : DependencyObject 
{ 
    // Interface Number 
    public int Interface 
    { 
     get { return (int)GetValue(InterfaceProperty); } 
     set { SetValue(InterfaceProperty, value); } 
    } 
    public static readonly DependencyProperty InterfaceProperty = 
     DependencyProperty.Register("Interface", typeof(int), typeof(ConfigWindowViewModel), new UIPropertyMetadata(0)); 

    } 

回答

5
newRb.SetBinding(RadioButton.IsCheckedProperty, binding); 

或者:

BindingOperations.SetBinding(radioButton, RadioButton.IsCheckedProperty, binding); 
+0

優秀 - 感謝 – Greg 2010-09-09 07:24:30

相關問題