3

我正在處理Windows Phone 7的照片庫,我試圖讓每個圖像都採用全屏並水平滑動。 我到目前爲止做的是使用我已修改爲水平滾動的列表框,但問題是,我似乎無法找到一種方法來綁定ListboxItem的寬度和高度與ActualWidth和ActualHeight列表框本身。 我想這樣做的原因是,如果手機的方向改變,照片的大小也會改變以適應屏幕。Silverlight - 綁定列表框ActualWidth到ListboxItem寬度

以下是到目前爲止我有(我已經使用TemplatedParent和的RelativeSource嘗試,但我必須做一些錯誤的,因爲它沒有在所有的工作)的代碼:上

<Style x:Key="PhotoGalleryItem" TargetType="ListBoxItem"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="ListBoxItem"> 
       <Grid x:Name="ListBoxItemRoot" HorizontalAlignment="Stretch" Margin="4,0,4,0" Width="{Binding RelativeSource={RelativeSource TemplatedParent},Path=ActualWidth}"> 
        <Image Source="{Binding Mode=OneWay}" /> 
       </Grid> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

<Style TargetType="controls:PhotoGallery"> 
    <Setter Property="Background" Value="Red"/> 
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/> 
    <Setter Property="BorderThickness" Value="0"/> 
    <Setter Property="BorderBrush" Value="Transparent"/> 
    <Setter Property="Padding" Value="0"/> 
    <Setter Property="ItemsPanel"> 
     <Setter.Value> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="controls:PhotoGallery"> 
       <Border BorderBrush="Transparent" BorderThickness="0" > 
        <Grid x:Name="LayoutRoot" Background="{TemplateBinding Background}"> 
         <ScrollViewer x:Name="Scroller" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Hidden" > 
           <ItemsPresenter/> 
         </ScrollViewer> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="ItemContainerStyle" Value="{StaticResource PhotoGalleryItem}" /> 
</Style> 

任何想法如何實現這個結果?

感謝

+0

您可以選擇添加以下代碼到你的應用程序頁面的構造函數? SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape; – 2010-09-05 22:01:52

回答

0

這可能是類似這樣的問題I asked before

Add HorizontalContentAlignment="Stretch" to your ListBox. 
That should do the trick. 

我看你已經有了「HCA」與二傳手設定,所以我不知道到底發生了什麼事。

+0

這不起作用 – swinefeaster 2011-12-31 21:33:30

+0

可能不在WP7上 - 我認爲它可以在Windows上運行 – 2012-01-04 04:05:58

0

你可以做到這一點的代碼:

public static void bindItemWidthToListBoxWidth(FrameworkElement cell) 
{ 
    ListBox parent = findListBoxParent(cell); 
    if(parent != null) 
    { 
    Binding binding = new Binding(); 
    binding.Source = parent; 
    binding.Path = new PropertyPath("ActualWidth"); 
    binding.Mode = BindingMode.OneWay; 

    cell.SetBinding(Grid.WidthProperty, binding); 
    } 
} 

public static ListBox findListBoxParent(DependencyObject el) 
{ 
    ListBox retValue = findAncestor<ListBox>(el); 
    return retValue; 
} 

public static tType findAncestor<tType>(DependencyObject el) 
    where tType : DependencyObject 
{ 
    tType retValue = null; 

    DependencyObject parent = VisualTreeHelper.GetParent(el); 

    if(parent != null) 
    { 
    if (parent is tType) 
    { 
     retValue = (tType)parent; 
    } 
    else 
    { 
     retValue = findAncestor<tType>(parent); 
    } 
    } 

    return retValue; 
}