0

我有这个通过表单重复的 WPF:

<WrapPanel local:RadioChecker.IsChecked="False">
    <RadioButton Content="Yes" Height="16" GroupName = "rbgSeizure" Name="rbSeizureYes" 
                    local:RadioChecker.IsChecked="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" />
    <RadioButton Content="No"  Height="16" GroupName = "rbgSeizure" Name="rbSeizureNo" 
                    local:RadioChecker.IsChecked="{Binding RelativeSource={RelativeSource Self}, Path=IsChecked}" />
</WrapPanel>

以相同方式设置每个 WrapPanel 和每个 RadioButton 的方法是什么,而不必将上面的内容复制并粘贴local:...到每个控件上?

我可以在后面的代码中获取每种类型的控件,例如:

GetLogicalChildCollection<RadioButton>(mainPanel)
    .ForEach(rb =>
    {
        Binding binding = new Binding();
        //binding.Source
    });

但我不确定如何设置绑定/将自定义附加属性附加到控件。

4

1 回答 1

1

应该是这样的:

GetLogicalChildCollection<RadioButton>(mainPanel).ForEach(
    rb =>
    {
        var binding = new Binding
        {
            Path = new PropertyPath(RadioButton.IsCheckedProperty),
            Source = rb
        };               
        rb.SetBinding(RadioChecker.IsCheckedProperty, binding);
    });

或这个:

GetLogicalChildCollection<RadioButton>(mainPanel).ForEach(
    rb => rb.SetBinding(RadioChecker.IsCheckedProperty,
                        new Binding("IsChecked") { Source = rb }));
于 2013-06-12T20:06:46.970 回答