【发布时间】:2010-09-09 07:21:09
【问题描述】:
如何在代码中以编程方式实现这一行 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));
}
【问题讨论】: