У меня проблема с изменением TemplatedParent из ControlTemplate. Мой код 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>
При изменении Фокусируемого в источнике также должно измениться Фокусируемое в целевом объекте и Должно измениться Включенное в исходном и целевом объекте.
Для примера:
source.Focucable = false
тогда
source.IsEnabled = false, target.IsEnabled = false, target.Focusable = false
ОБНОВЛЕНО
Когда я попробовал решение, предложенное Рэйчел, все работает нормально, пока код выглядит следующим образом:
<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>
и с #
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)