Silverlight - 绑定列表框ActualWidth到ListboxItem宽度

时间:2010-09-05 13:42:09

标签: silverlight windows-phone-7 binding listboxitem actualwidth

我正在为Windows Phone 7的照片库工作,我正在尝试让每张图像都采用全屏幕并水平滑动。 到目前为止我所做的是使用我已修改的列表框水平滚动,但问题是我似乎无法找到一种方法将ListboxItem的宽度和高度与Listbox的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>

关于如何实现这一结果的任何想法?

由于

2 个答案:

答案 0 :(得分:0)

这可能类似于这个问题I asked before

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

我看到你已经设置了'HCA'设置了一个setter,所以我不确定到底发生了什么。

答案 1 :(得分: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;
}