3

Я работаю над фотогалереей для Windows Phone 7, и я пытаюсь заставить каждое изображение снимать весь экран и скользить по горизонтали. Что я делаю до сих пор, так это использовать список, который я изменил для прокрутки по горизонтали, но проблема в том, что я не могу найти способ привязать ширину и высоту ListboxItem с помощью ActualWidth и ActualHeight Перечислите себя. Причина, по которой я хочу сделать это, заключается в том, что если ориентация телефона изменится, размер фотографий изменится, чтобы соответствовать экрану.Silverlight - Binding Listbox ActualWidth to ListboxItem Width

Ниже приводится код, который я получил до сих пор (я попытался с помощью 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

Вы добавив следующий код в конструктор вашей страницы приложения? Поддерживаемые ресурсы = Поддерживаемые параметры. Portrait | SupportedPageOrientation.Landscape; –

ответ

0

это может быть похоже на этот вопрос I asked before

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

Я вижу, у вас уже есть «ГЛК» установить с установщиком, так что я не знаю точно, что происходит.

+0

Это не работает, к сожалению, – swinefeaster

+0

, возможно, не на WP7 - я думаю, что он работает в Windows –

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; 
} 
Смежные вопросы