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>

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

4

2 回答 2

1

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

Binding NameOfProperty, Mode=TwoWay
于 2012-06-21T16:30:19.850 回答
1

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

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

对于诸如背景颜色之类的东西,这会很好。但是,当您想要一直绑定文本之类的内容时,您应该使用长手并更改模式:

Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}"
于 2012-06-21T16:35:06.267 回答