Silverlight - Binding Listbox ActualWidth to the width of ListboxItem

I am working on a photo gallery for Windows Phone 7, and I am trying to get each image to shoot the entire screen and slide horizontally. What I am doing so far is to use a list that I have changed to scroll horizontally, but the problem is that I cannot find a way to anchor the width and height of the ListboxItem using ActualWidth and ActualHeight from the list itself. The reason I want to do this is because if the orientation of the phone changes, the size of the photos will also change to fit the screen.

Below is the code that I still have (I tried using TemplatedParent and RelativeSource, but I have to do something wrong, since it does not work at all):

<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>

, ?

+3
2

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

, "HCA" , , .

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;
}
0

Source: https://habr.com/ru/post/1763326/


All Articles