【问题标题】:Custom Windows Phone user control自定义 Windows Phone 用户控件
【发布时间】:2013-07-05 07:58:41
【问题描述】:

我正在开发一个自定义 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 像素,但所有内容 - 橡胶(支持两个方向)

当我尝试将 d:DesignHeight="480" d:DesignWidth="480" 更改为 200px 时,它会使 ListBoxItem 变为 200px,但文本分散...

我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    您需要反转列定义:

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多