我從ControlTemplate修改TemplatedParent時遇到了問題。我的XAML代碼是:從ControlTemplate修改TemplatedParent
<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>
當調焦在源改變目標應該改變也可聚焦以源代碼和目標上啓用了應該改變。
對於excample:
source.Focucable =假
然後
source.IsEnabled =假,target.IsEnabled =假,target.Focusable =假
已更新
當我試圖通過瑞秋提出的解決方案,一切工作正常,只要代碼看起來是這樣的:我在實際應用中,在這裏我不能設置在XAML綁定發生
<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>
問題(控制是動態構建,和模板正在從資源文件中讀取),但只在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源調焦設置爲False,所以在C#約束力也應該設置的IsEnabled爲false。但是當我點擊收音機時,它正在檢查。我應該在哪裏應該把這個C#代碼使事情工作,因爲它應該是(我試過Window_Load,Radio_Initialized,Radio_Loaded)
Rachel如果您能再次閱讀我的問題,我已經做了一些更改。 – torpederos
@torpederos我對你想要做什麼感到困惑,但試着在'中設置綁定' –
Rachel
在XAML和C#中混合綁定對我來說不起作用 - 我不知道爲什麼。我通過爲FosusableChanged添加事件處理程序並設置了IsEnable屬性來解決我的問題。 – torpederos