应用样式后,绑定不再有效

时间:2012-06-21 15:59:59

标签: wpf

我有以下文本框,其中绑定工作正常,直到我添加样式:

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

应用样式后,文本框会填充但任何更改都不会发送回代码。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

您是否尝试在控件模板上将绑定设置为双向?

Binding NameOfProperty, Mode=TwoWay

答案 1 :(得分:1)

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

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

对于背景颜色等等,这样可以正常工作。但是当你想要文本绑定的东西时,你应该使用长手并改变模式:

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