Reputation: 841
I have a problem to modify TemplatedParent from ControlTemplate. My xaml code is:
<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>
When Focusable in source is changing also Focusable in target should change and Enabled in source and target should change.
For excample:
source.Focucable = false
then
source.IsEnabled = false, target.IsEnabled=false, target.Focusable=false
UPDATED
When I tried solution proposed by Rachel, everything works fine as long as code looks like this:
<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>
Problem occurs in my real application, where I can't set binding in xaml (controls are build dynamically, and templates are reading from resources file) but only in c#.
XAML in this situation looks like this:
<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>
and c#
Binding bindingRadio = new Binding("RadioButton.Focusable");
bindingRadio.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
source.SetBinding(RadioButton.IsEnabledProperty, bindingRadio);
In xaml source Focusable is set to False, so binding in c# should set IsEnabled also to false. But when I click on radio it is making checked. Where should I put this c# code to make things works as it should be (i tried Window_Load, Radio_Initialized, Radio_Loaded)
Upvotes: 0
Views: 866
Reputation: 132618
Why not just bind the IsEnabled
properties to the Focusable
properties?
It looks like you're already binding the Focusable
property to the template value, so just bind the IsEnabled
property to the Focusable
one
IsEnabled="{Binding Focusable, RelativeSource={RelativeSource Self}}"
Also, I think as a shortcut for your Focusable
binding you can use {TemplateBinding Focusable}
instead of binding to the relative source
Upvotes: 1