2012-06-21 121 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