我有3列的WPF列表视图。名称,成本和出售。成本和销售是可编辑的文本框,格式为货币。
2个问题: -我可能列出了一些特定项目,其中需要将成本和销售的格式设置为百分比而不是货币格式。因此,如果Name =“ x”,则成本和销售应具有百分比stringformt。
我将如何在WPF中实现它?我一直在研究DataTriggers,但不知道如何正确实现。
<ListView x:Name="Pricing_LV" HorizontalAlignment="Left" Height="335" Margin="10,41,0,0" VerticalAlignment="Top" Width="350" TabIndex="22">
<ListView.View>
<GridView>
<GridViewColumn Header="Surcharge" Width="185" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Cost" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="55" Text="{Binding Path=Cost, StringFormat='c'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Sell" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="55" Text="{Binding Path=Sell, StringFormat='c'}" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:-1)
您可能已经尝试将DataTrigger
放在TextBox.Triggers
内,但随后出现以下错误
错误:触发器集合成员的类型必须为EventTrigger
另请参阅此question
解决方案是将Trigger
包裹在Style
内
<ListView x:Name="Pricing_LV" HorizontalAlignment="Left" Height="335" Margin="10,41,0,0" VerticalAlignment="Top" Width="350" TabIndex="22">
<ListView.View>
<GridView>
<GridViewColumn Header="Surcharge" Width="185" DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Header="Cost" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="55">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Text" Value="{Binding Path=Cost, StringFormat='c'}"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Name}" Value="x">
<Setter Property="Text" Value="{Binding Path=Cost, StringFormat=#.00\\%}"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Name}" Value="y">
<Setter Property="Text" Value=""></Setter>
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Sell" Width="70">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBox Width="55">
<TextBox.Style>
<Style TargetType="TextBox">
<Setter Property="Text" Value="{Binding Path=Sell, StringFormat='c'}"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Name}" Value="x">
<Setter Property="Text" Value="{Binding Path=Sell, StringFormat=#.00\\%}"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
注意:我不使用p
作为百分比格式,因为我认为这不适用于您的情况。另请参见here