3

我正在为 Windows Phone 7 制作照片库,我试图让每张图片都占据全屏并水平滑动。到目前为止,我正在做的是使用我已经修改为水平滚动的列表框,但问题是我似乎无法找到将 ListboxItem 的宽度和高度与列表框的 ActualWidth 和 ActualHeight 绑定的方法本身。我想这样做的原因是,如果手机的方向改变,照片的大小也会改变以适应屏幕。

以下是我到目前为止的代码(我尝试使用 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>

关于如何达到这个结果的任何想法?

谢谢

4

2 回答 2

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;
}
于 2011-12-31T21:44:38.590 回答
0

这可能类似于我之前问过的这个问题

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

我看到你已经用二传手设置了“HCA”,所以我不确定到底发生了什么。

于 2010-09-05T18:24:35.660 回答