2013-07-05 81 views
1

我正在開發一個自定義列表框項目模板,到目前爲止,寫了這個代碼:自定義Windows Phone的用戶控制

<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,但文字dissapers ...

我做錯了什麼?

回答

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>