2013-07-16 147 views
2
  1. 我有一个MainWindow.XAML它
  2. 文本框的文本装订完成
  3. 当我里面添加(在MainWindow.XAML)它的工作原理
  4. 绑定的StringFormat
  5. 文本框添加
  6. 当我里面添加样式的StringFormat,它不工作
下面

是从风格和MainWindow.xaml文本框文本格式从样式

0码
<TextBox Grid.Row="1" Grid.Column="4" Style="{StaticResource TextBoxStyle}" Text="{Binding CustomerAmount,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" HorizontalAlignment="Stretch" Margin="10,0,0,0"/> 


<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}" > 
     <Setter Property="Text" Value="{Binding Text, RelativeSource={RelativeSource Self},StringFormat='#,###,###,##0.00;(#,###,###,##0.00)'}"></Setter> 
    </Style> 

回答

4

,那么您已经基本适用 Text结合现在,一个在主窗口和一个在Style

Text财产上的控制MainWindow.xaml设置有超过你在Style设置一个优先级,所以StringFormat您通过Style设置实际上是永远不会应用COS的是整个Style.Setter被忽略。

一个非常粗略的方式,使这项工作,证明上述声明是试图XAML中切换到跟随,

<TextBox Grid.Row="1" Grid.Column="4" Style="{StaticResource TextBoxStyle}" Tag="{Binding CustomerAmount,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" HorizontalAlignment="Stretch" Margin="10,0,0,0"/> 

和风格:

<Style x:Key="TextBoxStyle" 
     TargetType="{x:Type TextBox}"> 
    <Setter Property="Text" 
      Value="{Binding Tag, 
          RelativeSource={RelativeSource Self}, 
          StringFormat='#,###,###,##0.00;(#,###,###,##0.00)', 
          Mode=TwoWay, 
          UpdateSourceTrigger=PropertyChanged}" /> 
</Style> 

这将工作,因为你现在有Tag在MainWindow中绑定,TextStyle中绑定。您可以切换到自定义附加属性或DP来获得相同的行为

+0

嗨感谢您的投入。在发布这个问题之前,我也尝试在Style中使用Converter。现在我明白了它没有发生的原因。 – user1386121