0

I have a WPF listview with 3 columns. Name, cost, and sell. Cost and Sell are editable textboxes formatted as currency.

2 issues: - I might have specific items listed where the cost and sell need to be formatted as a percentage instead of currency. So if Name="x" then cost and sell should have percentage stringformt.

  • Some items only require a sell option. so if Name="y", do not display textbox in Cost column.

How would I implement this in WPF? I was looking into DataTriggers but couldn't figure out how to implement correctly.

<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>
plori
  • 331
  • 3
  • 7
Mersad
  • 17
  • 3

1 Answers1

-1

You may have tried putting your DataTriggers direct inside TextBox.Triggers but then getting the following error

Error: Triggers collection members must be of type EventTrigger

See also this question

The solution is to simply wrap your Triggers inside a 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>

Note: I don't use p for percentage format because I think this won't suit in your case. See also here

nosale
  • 808
  • 6
  • 14