从ControlTemplate修改TemplatedParent

时间:2011-10-18 13:07:18

标签: c# wpf xaml binding

我有一个问题需要从ControlTemplate修改TemplatedParent。我的xaml代码是:

<RadioButton Name="source" Focusable="True">
    <RadioButton.Template>
        <ControlTemplate>                       
            <RadioButton Name="target" Focusable="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Focusable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />                       
        </ControlTemplate>                  
    </RadioButton.Template>
</RadioButton>

当源中的Focusable正在变化时,目标中的可聚焦应该更改并且源和目标中的已启用应该更改。

例如:

  

source.Focucable = false

     

然后

     

source.IsEnabled = false,target.IsEnabled = false,target.Focusable = false

已更新

当我尝试Rachel提出的解决方案时,只要代码如下所示,一切正常:

<RadioButton Name="source" 
                 Focusable="False" 
                 IsEnabled="{Binding Focusable, RelativeSource={RelativeSource Self}}"
                 Height="19" 
                 HorizontalAlignment="Left"                  
                 Width="146" Margin="0,12,0,0" VerticalAlignment="Top">
        <RadioButton.Template>
            <ControlTemplate>
                <RadioButton 
                    Name="target" 
                    Focusable="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Focusable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    IsEnabled="{Binding Focusable, RelativeSource={RelativeSource Self}}" 
                    IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </ControlTemplate>
        </RadioButton.Template>
    </RadioButton>

在我的实际应用程序中出现问题,我无法在xaml中设置绑定(控件是动态构建的,模板是从资源文件中读取的),但仅限于c#。

这种情况下的XAML如下所示:

<RadioButton Name="source" 
                 Focusable="False"               
                 Height="19" 
                 HorizontalAlignment="Left"                  
                 Width="146" Margin="0,12,0,0" VerticalAlignment="Top">
        <RadioButton.Template>
            <ControlTemplate>
                <RadioButton 
                    Name="target" 
                    Focusable="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Focusable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                    IsEnabled="{Binding Focusable, RelativeSource={RelativeSource Self}}" 
                    IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            </ControlTemplate>
        </RadioButton.Template>
    </RadioButton>

和c#

Binding bindingRadio = new Binding("RadioButton.Focusable");
bindingRadio.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
source.SetBinding(RadioButton.IsEnabledProperty, bindingRadio);

在xaml源代码中,Focusable设置为False,因此在c#中绑定也应将IsEnabled设置为false。但是当我点击收音机时它正在检查。我应该在哪里放置这个c#代码以使其工作正常(我试过Window_Load,Radio_Initialized,Radio_Loaded)

1 个答案:

答案 0 :(得分:1)

为什么不将IsEnabled属性绑定到Focusable属性?

看起来您已经将Focusable属性绑定到模板值,因此只需将IsEnabled属性绑定到Focusable一个

IsEnabled="{Binding Focusable, RelativeSource={RelativeSource Self}}"

此外,我认为作为Focusable绑定的快捷方式,您可以使用{TemplateBinding Focusable}而不是绑定到相对来源