自定义Windows Phone用户控件

时间:2013-07-05 07:58:42

标签: xaml windows-phone-7 user-controls windows-phone-8

我正在开发一个自定义ListBox项目模板,到目前为止编写了这段代码:

<UserControl x:Class="Application.Test"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Image HorizontalAlignment="Right" Height="100" Grid.RowSpan="2" Grid.Column="1" VerticalAlignment="Center" Width="100" Source="ControlTemplates\arrow_white.png"/>
    <TextBlock Name="Title" TextWrapping="Wrap" Grid.Row="1" Grid.Column="0" Height="auto" Width="auto" Grid.ColumnSpan="2" Margin="10,212,119,214"/>
</Grid>

链接到图片示例:http://s7.postimg.org/oeyl8gge3/Capture2.png

我想让这个ListBoxItem 200 px,但所有内容 - 橡胶(支持两种方向)

当我尝试更改d:DesignHeight =“480”d:DesignWidth =“480”到200px时,它会使ListBoxItem 200px但文本消失...

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您需要反转列定义:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="*"/>
    <ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

这样,右列将具有固定大小,因为它会自动调整大小以适应具有固定宽度的Image控件。现在,第一列将始终拉伸以使用所有空间。

然而,这还不够。每个列表框项都有自己的宽度。要确保每个项目具有最大宽度,请使用以下代码:

<ListBo>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>
相关问题