[英]How can I implement this one line XAML programmatically in WPF code?
如何在代码中以编程方式实现这一行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));
}
newRb.SetBinding(RadioButton.IsCheckedProperty, binding);
或者:
BindingOperations.SetBinding(radioButton, RadioButton.IsCheckedProperty, binding);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.