繁体   English   中英

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

[英]Bind Custom Attached Property in Code or Styles/Templates

我有通过表格重复的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 }));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM