wpf - 应用样式后绑定(bind)不再起作用

标签 wpf

我有以下文本框,在添加样式之前,绑定(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/

相关文章:

C# ObservableCollection OnCollectionChanged 项目更改时不触发

c# - 一次移动两个 WPF 窗口?

c# - c++/cli 仅将内部成员公开给托管类

wpf - 为什么在使用 MSBuild 4.0 编译 .NET3.5 项目时出现 BadImageFormatException

wpf - 打开 PDF : WPF

c# - 如何确定 WPF 窗口是否打开?

c# - 如何使用 MvvM 应用依赖注入(inject)?

c# - 使用实时图表创建 WPF C# 图表

c# - 我可以在 WPF Canvas 中使用较低 ZIndex 的 HitTest 元素吗?

c# - 如何删除 WPF 中的系统菜单?