【问题标题】:How can I implement this one line XAML programmatically in WPF code?如何在 WPF 代码中以编程方式实现这一行 XAML?
【发布时间】: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));

   }

【问题讨论】:

    标签: c# .net wpf xaml binding


    【解决方案1】:
    newRb.SetBinding(RadioButton.IsCheckedProperty, binding);
    

    或者:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多