Как я могу реализовать этот однострочный XAML программно в коде WPF? - PullRequest
1 голос
/ 09 сентября 2010

Как я могу программно реализовать этот однострочный 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));

   }

1 Ответ

5 голосов
/ 09 сентября 2010
newRb.SetBinding(RadioButton.IsCheckedProperty, binding);

В качестве альтернативы:

BindingOperations.SetBinding(radioButton, RadioButton.IsCheckedProperty, binding);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...