【问题标题】:How do I achieve a 2 -way data bind with an applied style in xaml?如何使用 xaml 中的应用样式实现 2 路数据绑定?
【发布时间】:2013-03-28 09:45:59
【问题描述】:

我的 page.xaml 中有这段代码

<TextBox x:Name="NameTextField" Grid.ColumnSpan="7" Grid.Column="1" Text="{Binding Name, Mode=TwoWay}" Style="{StaticResource TextBoxStyle}" />

指的是这种风格:

      <Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid x:Name="grid" Height="55" Background="White">
                    <Rectangle Stroke="#FFD9D9D9" StrokeThickness="6"/>
                    <ContentPresenter x:Name="contentPresenterText" VerticalAlignment="Center" Margin="6,0" Height="42" >
                        <TextBox Text="{TemplateBinding Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
                    </ContentPresenter>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
  </Style>

这在从绑定中预填充数据时可以正常工作,但在输入数据时似乎无法以其他方式工作。

我在这里遗漏了什么明显的东西吗?

非常感谢

【问题讨论】:

    标签: xaml windows-8 blend


    【解决方案1】:

    尝试改变:

    <TextBox Text="{TemplateBinding Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
    

    到:

    <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=Text}" FontSize="21.333" FontFamily="Arial" FontWeight="Bold"/>
    

    TemplateBinding 似乎默认为单向绑定。

    【讨论】:

      猜你喜欢
      • 2015-11-11
      • 2010-12-19
      • 1970-01-01
      • 2019-10-12
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多