[英]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.