[英]Windows Phone xaml markup
此示例是否有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>
您可以尝试以下方法:将TextBlock MaxWidth绑定到Grid的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竞赛已经结束了?:)
通过编写“自定义面板”解决了这个问题:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.