我有以下文本框,在添加样式之前,绑定(bind)工作正常:
<TextBox Text="{Binding SelectedGroupPolicyTermSummary.ImportantInfo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
HorizontalAlignment="Stretch" AcceptsReturn="True"
IsReadOnly="{Binding IsEditable, Converter={StaticResource InvertedBoolConverter}}"
Foreground="Red" TextWrapping="Wrap">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="TextBox.IsReadOnly" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<TextBox Text="{TemplateBinding Text}"
VerticalAlignment="Bottom"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Style="{DynamicResource SelectableTextStyle}"
TextWrapping="{TemplateBinding TextWrapping}"
Foreground="{TemplateBinding Foreground}"
Width="{TemplateBinding Width}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="Auto"/>
</Trigger>
<Trigger Property="TextBox.IsReadOnly" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}" >
<TextBox Text="{TemplateBinding Text}"
VerticalAlignment="Bottom"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Style="{DynamicResource TextBoxStyle}"
TextWrapping="{TemplateBinding TextWrapping}"
Foreground="{TemplateBinding Foreground}"
Width="{TemplateBinding Width}"
AcceptsReturn="{TemplateBinding AcceptsReturn}">
</TextBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
应用样式后,文本框会填充,但任何更改都不会发送回代码。有什么想法吗?
最佳答案
TemplateBinding 标记扩展基本上提供以下绑定(bind):
{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay}
对于背景颜色之类的东西,这会很好用。但是,当您想要像文本一样始终绑定(bind)时,您应该使用长手并更改模式:
Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
关于wpf - 应用样式后绑定(bind)不再起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11142104/