在银光 2....
我的 xaml 代码中有一个 RadioButton,如下所示:
<RadioButton GroupName="Gender" Content="Male" IsChecked="{Binding Path=Gender, ConverterParameter=1, Mode=TwoWay, Converter={StaticResource RadioStringConverter}}" Width="49" HorizontalAlignment="Left"/>
这很好用。我的问题是尝试动态复制此功能。
RadioButton rb = new RadioButton() {GroupName = "Gender", Content = "Male" ,Width = (double)49,HorizontalAlignment = System.Windows.HorizontalAlignment.Left};
这可行,但是当我尝试将转换器放入时,它会损坏。这样做的正确方法是什么?有什么好的工作例子吗?
这是我尝试过的......
RadioButton rb = new RadioButton() {GroupName = "Gender", Content = "Male" ,Width = (double)49,HorizontalAlignment = System.Windows.HorizontalAlignment.Left};
RadioStringConverter rsc = new RadioStringConverter();
Binding binding = new Binding(layout.FieldName) { Source = mainLayout.DataContext, Mode = BindingMode.TwoWay,ConverterParameter = 1,Converter = rsc}; // to emulate the "{StaticResource RadioStringConverter}"};
rb.SetBinding(RadioButton.IsCheckedProperty, binding);
sp.Children.Add(rb);
虽然这编译得很好,但它不能正确运行。1)如何动态引用静态资源?2) 如何将此静态资源动态添加到 XAML?现在我有这个参考硬编码。
我是否让这变得比需要的更困难?