下面是style和MainWindow.xaml的代码
<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>
答案 0 :(得分:4)
您现在基本上已经应用了两个 Text
绑定,一个在MainWindow,另一个在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>
这将有效,因为您现在在MainWindow中绑定了Tag
,在Text
中绑定了Style
。您可以切换到自定义附加属性或DP以获得相同的行为