0

I would like to use a list box to have a label with 3 parts. The first part is hard-coded text, the second part is a value from a bound property in the view model, and the last part is a hard-coded text too.

The idea is something like that:

<ListBox x:Name="list1" ItemsSource="{Binding IvasConImportes}"
    BorderBrush="Transparent"
    BorderThickness="0"
    Background="Transparent">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Label Name="lblIvaParte01" Content="TAX " HorizontalAlignment="Left" HorizontalContentAlignment="Right" Width="6.4cm" Margin="0,0,0,0" Padding="0,0,0,0" VerticalAlignment="Top"/>
                <Label Name="lblIvaParte02" Content="{Binding Item1}" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Width="6.4cm" Margin="0,0,0,0" Padding="0,0,0,0" VerticalAlignment="Top"/>
                <Label Name="lblIvaParte03" Content="%" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Width="6.4cm" Margin="0,0,0,0" Padding="0,0,0,0" VerticalAlignment="Top"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

The problem is that in this way to align the text is a bit hard and also I guess that it has to be possible to do all this with only one label, but I don't know the syntax in the binding of the content of the label to do that.

dymanoid
  • 14,771
  • 4
  • 36
  • 64
Álvaro García
  • 18,114
  • 30
  • 102
  • 193

1 Answers1

1

Use a TextBlock with three Run elements inside it. Each run can have its text property set as a constant or be data-bound.

<TextBlock>
    <Run Text="TAX" />
    <Run Text="{Binding Item1}" />
    <Run Text="%"/>
</TextBlock>
Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • Note that `Run` binds `TwoWay` by default. If `Item1` is a get-only property, you'll get an exception. – dymanoid Oct 29 '18 at 10:49
  • Thanks. And yes, the propery is read only, but I can change the binding mode to one way and solve the problem. Thanks for the tip. – Álvaro García Oct 29 '18 at 11:09