1

I would like to format a textblock which is bound to a value, to show "R" before the actual value, is this possible, cause I cannot directly change the value?

Thank you

<ListBox x:Name="lstbundleListbox"
         Foreground="White"
         Height="320"
         HorizontalAlignment="Center">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalContentAlignment"
              Value="Center" />
    </Style>
  </ListBox.ItemContainerStyle>
  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock Text="{Binding name}"
                   TextWrapping="Wrap"
                   HorizontalAlignment="Center" />

        <TextBlock Text="{Binding cost}"
                   TextWrapping="Wrap"
                   HorizontalAlignment="Center" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel Orientation="Vertical" />
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
</ListBox>

So I basically want the textblock to show R(cost)

4

2 回答 2

8

利用Run

<TextBlock>
    <Run Text="R" />
    <Run Text="{Binding cost}" />
</TextBlock>

或使用StringFormat

<TextBlock Text="{Binding cost, StringFormat=R{0}}" />
于 2013-07-02T10:49:10.363 回答
0

我认为这可以通过

  <StackPanel Orientation Horizontal>
       <TextBlock Text="R(" />
       <TextBlock Text="{Binding cost}"/>
       <TextBlock Text=")" />
   </StackPanel>

它们之间的距离可以通过设置文本块的填充来设置......

于 2013-07-02T11:02:38.010 回答