2013-03-28 32 views
1

我有這段代碼在我page.xaml如何在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> 

該工程確定從綁定時預填充數據,但似乎並不在數據輸入時以另一種方式工作。

有什麼明顯的我在這裏失蹤?

非常感謝

回答

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似乎默認爲單向綁定。

相關問題