在代码或样式/模板中绑定自定义附加属性



我有一个重复的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
    });

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

应该像这样:

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 }));

最新更新