2011-03-21 51 views
1

我有一個列表框模板,顯示一個圖像和三個文本框。你會看到他們如何分配在下面的代碼。我的問題是有些項目沒有圖像,我希望文本填充整行。 我試過不使用網格,使用畫布,但我不知道爲什麼,當在列表框中使用畫布時,什麼都不顯示。我不知道這是否容易完成。代碼如下:列表框模板,隱藏元素

<ListBox Grid.Column="0" Grid.Row="1" Background="White" HorizontalAlignment="Stretch" Name="itemList" VerticalContentAlignment="Stretch" SelectionChanged="listBoxSetmana_SelectionChanged"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinWidth="480"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="28" /> 
        <RowDefinition Height="17" /> 
        <RowDefinition Height="50" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="60" /> 
        <ColumnDefinition Width="*" /> 
       </Grid.ColumnDefinitions> 

       <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Grid.RowSpan="3" Grid.Column="0" Grid.Row="0" BorderThickness="1" BorderBrush="#FFFF003F" Padding="1"> 
        <Image HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="listImage" Width="36" Height="36" Source="{Binding thumbnail}" /> 
       </Border> 
       <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="0" Name="title" Foreground="Black" Text="{Binding title}" FontWeight="Bold" FontSize="20" /> 
       <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="1" Name="published" Foreground="Black" Text="{Binding published}" FontSize="13" /> 
       <TextBlock Padding="0 0 25 0" Grid.Column="1" Grid.Row="2" Name="subtitle" Foreground="Black" Text="{Binding subtitle}" FontSize="16" TextWrapping="Wrap" /> 
       <TextBlock Visibility="Collapsed" Text="{Binding id}" /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

謝謝!

+1

我不能看到你在你所提供的代碼中使用'Canvas',但** **填充值應該是逗號分隔,不佔空間:'填充=「0, 0,25,0「' – 2011-03-21 15:37:07

+0

感謝您指出逗號缺少 – enkara 2011-03-22 08:54:23

回答

1

有幾種方法可以將它們綁定在一起。

的一種方式是:

  • 添加一些填充到邊框
  • 綁定邊框縮略圖屬性的使用適當ValueConverter
  • 變化列0的從「60」的寬度的可見性到「自動」 - 當圖像被摺疊時它將消失。
+0

我已經找到了關於如何使用轉換器的示例(http://stackoverflow.com/questions/4695057/formatting-a-date-in-xaml-on -wp7),但我的xaml無法識別單詞命名空間。如何將轉換器定義爲資源? – enkara 2011-03-22 09:07:22

+1

你需要定義你的命名空間 - 在你的xaml的第一個聲明中,添加類似xmlns:mynamespace =「clr-namespace:MyProject.Converters」,這將允許你使用「mynamespace:MyConverter」來包含你的轉換器 – Stuart 2011-03-22 09:14:25

+0

太棒了!感謝您的幫助。我想我需要一本關於silverlight的好手冊 – enkara 2011-03-22 09:25:50