2012-06-21 196 views
2

后不绑定不再工作,我有以下文本框在那里工作得很好的结合,直到我添加的样式:应用样式

<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> 

我应用该样式的文本框中填入后,但任何更改都不会发回到代码。任何想法为什么?

回答

1

的TemplateBinding标记扩展基本上提供以下绑定:

{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=OneWay} 

类的东西的背景颜色和诸如此类的东西,这将很好地工作。但是当你想要像Text一样的东西一路通过,你应该使用长期并改变模式:

Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" 
+0

非常感谢你,这解决了问题:) – soaloa

1

您是否尝试过将绑定设置为控件模板上的两种方式?

Binding NameOfProperty, Mode=TwoWay