2012-09-20 134 views
0

此示例中是否有XAML解決方案?我對全名和小點顯示的部分感興趣。特別是,如果姓名有足夠的空間(第一項),我不能在完整名稱後面加點小數。Windows Phone xaml標記

現在,我有這個,但因爲點總是在正確的它不適合:

<Grid Grid.Row="0" Grid.Column="1"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
      <ColumnDefinition Width="Auto"/> 
     </Grid.ColumnDefinitions> 
     <TextBlock Text="{Binding Path=Title}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
      Margin="0,-16,0,0" 
      Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
     <TextBlock Grid.Column="1" HorizontalAlignment="Right" 
      Text="{Binding Path=IsOnline, Converter={StaticResource StatusFormatter}}" Opacity="0.6" 
      Style="{StaticResource PhoneTextLargeStyle}"/> 
    </Grid> 

Sample

+2

爲了避免downvotes,後期[你試過了什麼](http://whathaveyoutried.com)。 – Adam

+0

如果添加間隔符ColumnDefinition,它應該可以工作。我無法測試它,因此我不會將它作爲答案發布,但語法如下: ,並使您的文本在列0和您的'點'在列2 –

+0

不起作用。如果姓名太長,點將消失 – Buddy

回答

0

通過編寫自定義面板解決了這個問題:?

public class MyStackPanel : Panel 
{ 
    protected override Size MeasureOverride(Size availableSize) 
    { 
     Size newSize = new Size(); 
     UIElement firstChild = Children[0]; 
     UIElement secondChild = Children[1]; 
     secondChild.Measure(availableSize); 
     firstChild.Measure(new Size(availableSize.Width - secondChild.DesiredSize.Width, availableSize.Height)); 
     newSize.Width = firstChild.DesiredSize.Width + secondChild.DesiredSize.Width; 
     newSize.Height = firstChild.DesiredSize.Height; 
     return newSize; 
    } 

    protected override Size ArrangeOverride(Size finalSize) 
    { 
     UIElement firstChild = Children[0]; 
     UIElement secondChild = Children[1]; 
     firstChild.Arrange(new Rect(0, 0, firstChild.DesiredSize.Width, firstChild.DesiredSize.Height)); 
     secondChild.Arrange(new Rect(firstChild.DesiredSize.Width, 0, secondChild.DesiredSize.Width, 
            firstChild.DesiredSize.Height)); 
     return base.ArrangeOverride(finalSize); 
    } 
} 

和XAML:

<local:MyStackPanel Grid.Column="1"> 
    <TextBlock Text="{Binding Path=Title}" 
       Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
    <TextBlock Text="{Binding Path=IsOnline, Converter={StaticResource StatusFormatter}}" 
       Opacity="0.6" 
       Style="{StaticResource PhoneTextLargeStyle}"/> 
</local:MyStackPanel> 
1

你可以試試下面的辦法:綁定的TextBlock MaxWidth到網格減去Widtd點的寬度(例如10個像素)。有兩種變體:使用轉換器或元素綁定。 這裏是如何做到這一點使用元件結合:

首先,我們需要與所需的寬度創建元素:

<Grid Grid.Row="0" Grid.Column="1"> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="10"/> 
    </Grid.ColumnDefinitions> 
    <Border x:Name="TextMaxWidth"/> <!-- this ActualWidth will be equals Grid.Width - 10 --> 
</Grid> 

然後綁定將TextBlock MaxWidth邊境ActualWidth

<Grid Grid.Row="0" Grid.Column="1"> 
    <Grid.ColumnDefinitions> 
    <!-- note that columns widths are changed; also you can use stack panel instead of grid --> 
     <ColumnDefinition Width="Auto"/> 
     <ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinitions> 
    <TextBlock Text="{Binding Path=Title}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" 
     <!-- this will restrict texblock from growing more than Grid.Width - 10 --> 
     MaxWidth="{Binding ActualWidth,ElementName=TextMaxWidth}" 
     Margin="0,-16,0,0" 
     Style="{StaticResource PhoneTextExtraLargeStyle}" /> 
    <TextBlock Grid.Column="1" HorizontalAlignment="Right" 
     Text="{Binding Path=IsOnline, Converter={StaticResource StatusFormatter}}" Opacity="0.6" 
     Style="{StaticResource PhoneTextLargeStyle}"/> 
</Grid> 

而且您可以使用轉換器將TextBlock MaxWidth直接綁定到網格ActualWidth並減去點的寬度。

Offtop:我認爲,VK比賽已經結束了:)

+0

是的,比賽已結束,但我想完成這東西。 – Buddy

+0

爲什麼邊界從第二個代碼示例中消失了,而列定義是不同的? – Buddy

+0

@Buddy第二個代碼示例是第一個代碼的延續,您應該一起使用它 – Alexander