How can I implement this one line XAML programmatically in WPF code?
How can I implement this one line XAML programmatically in code? (as I'm needing to create the radio buttons on the fly, but what them to having bindings)
<RadioButton IsChecked="{Binding Path=Mode, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Proxy}">Proxy</RadioButton>
I've got it to here (see below) so far, but now I'm struggling re how to get the binding to be hooked up. I've made a guess/assumption I should be using the Binding class...
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);
}
And for background the model class with dependency object:
public class ConfigWindowViewModel : DependencyObject
{
// Interface Number
public int Interface
{
get { return (int)GetValue(InterfaceProperty); }
set { SetValue(InterfaceProperty, v开发者_如何转开发alue); }
}
public static readonly DependencyProperty InterfaceProperty =
DependencyProperty.Register("Interface", typeof(int), typeof(ConfigWindowViewModel), new UIPropertyMetadata(0));
}
newRb.SetBinding(RadioButton.IsCheckedProperty, binding);
Alternatively:
BindingOperations.SetBinding(radioButton, RadioButton.IsCheckedProperty, binding);
精彩评论