List overflow

There's an overflow in the list. If anyone has an idea of ​​what I'm doing wrong in this part of the code, I check this problem, but not a very good answer.

ListView doesn't work either, I'll try it with ItemControl, but it's not so easy to get the selected item

Maybe with Zindex? But doesn't work either

enter image description here

 <UserControl x:Class="XX.ThumbnailView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Thumbnail="clr-namespace:XX.Thumbnail"
    xmlns:controls="clr-namespace:XX.Controls"
    xmlns:resources="clr-namespace:X.Resources">

   <UserControl.Resources>
      <DataTemplate x:Key="Thumb">
         <Grid Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" >
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Thumbnail:Thumbnail Grid.Column="0" Source="{Binding}" />
         </Grid>
      </DataTemplate>

      <Style TargetType="ListBox">
         <Setter Property="ItemsPanel">
            <Setter.Value>
               <ItemsPanelTemplate>
                  <StackPanel Orientation="Horizontal"
                      VerticalAlignment="Center"
                      HorizontalAlignment="Center"/>
               </ItemsPanelTemplate>
            </Setter.Value>
         </Setter>
      </Style>

   </UserControl.Resources>

   <controls:CustomDialog HorizontalAlignment="Center" VerticalAlignment="Center" Style="{StaticResource CustomDialogInformationStyle}" >
      <DockPanel>
         <Grid>
            <Grid.RowDefinitions>
               <RowDefinition Height="*" />
               <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>


            <ListBox Grid.Row="0" Name="ListBoxWindow" ItemsSource="{Binding}" ItemTemplate="{StaticResource Thumb}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
   ScrollViewer.VerticalScrollBarVisibility="Disabled" IsSynchronizedWithCurrentItem="True" Margin="0,0,0,20" Height="200" Width="300" >

               <ListBox.Template>
                  <!--Does anyone know a better way to turn off horizontal scrolling in a ListBox?-->
                  <ControlTemplate TargetType="ListBox" 
                                                                                 xmlns:s="clr-namespace:System;assembly=mscorlib">
                     <Border BorderBrush="{TemplateBinding Border.BorderBrush}" BorderThickness="{TemplateBinding Border.BorderThickness}" Name="Bd" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True" Padding="1,1,1,1">
                        <ScrollViewer VerticalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Auto" Focusable="False" Padding="{TemplateBinding Control.Padding}">
                           <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
                        </ScrollViewer>
                     </Border>
                     <ControlTemplate.Triggers>
                        <Trigger Property="UIElement.IsEnabled">
                           <Setter Property="Panel.Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                           <Trigger.Value>
                              <s:Boolean>False</s:Boolean>
                           </Trigger.Value>
                        </Trigger>
                        <Trigger Property="ItemsControl.IsGrouping">
                           <Setter Property="ScrollViewer.CanContentScroll">
                              <Setter.Value>
                                 <s:Boolean>False</s:Boolean>
                              </Setter.Value>
                           </Setter>
                           <Trigger.Value>
                              <s:Boolean>True</s:Boolean>
                           </Trigger.Value>
                        </Trigger>
                     </ControlTemplate.Triggers>
                  </ControlTemplate>
               </ListBox.Template>
            </ListBox>

            <Grid Grid.Row="1" HorizontalAlignment="Right">
               <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="Auto" />
               </Grid.ColumnDefinitions>
               <Button Grid.Column="0" Name="btnOk" Click="OkClick" Content="{resources:Translate OK}" Style="{StaticResource ButtonStyle}" />
               <Button Grid.Column="1" Name="btnCancel" Click="CancelClick" Content="{resources:Translate Annuler}" Style="{StaticResource ButtonStyle}" />
            </Grid>

            <!--<ContentControl Content="{Binding Path=/}" ContentTemplate="{StaticResource Thumb}" Width="458" Margin="0,0,0,20"/>-->
         </Grid>
      </DockPanel>
   </controls:CustomDialog>
</UserControl>

Sketch style

  <DataTemplate x:Key="Thumb">
         <Grid Margin="10" HorizontalAlignment="Center" VerticalAlignment="Center" >
            <Grid.ColumnDefinitions>
               <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Thumbnail:Thumbnail Grid.Column="0" Source="{Binding}" />
         </Grid>
      </DataTemplate>

And here is the UserControl icon for thumbnails

   public partial class ThumbnailView : UserControl
    {
      #region Private Members
      private readonly ModalBehavior _modalBehavior;
      private List<Window> _activeWindows;

      private Dictionary<IntPtr, Window> _active;
      #endregion

      #region Public Properties
      public Button BtnOK
      {
         get { return this.btnOk; }
      }

      public Button BtnCancel
      {
         get { return this.btnCancel; }
      }
      #endregion

       private void OkClick(object sender, RoutedEventArgs args)
       {
         _modalBehavior.SetModalBehaviorStatusOk();

       }

      private void CancelClick(object sender, RoutedEventArgs args)
      {
         _modalBehavior.SetModalBehaviorStatusCancel();
      }


       public ThumbnailView(IEnumerable<Window> activeWindows)
       {
          InitializeComponent();
         _activeWindows = new List<Window>();
         _active = new Dictionary<IntPtr, Window>();
         _activeWindows.AddRange(activeWindows);

         _modalBehavior = new ModalBehavior(this);

          this.btnCancel.Focus();
         //stBoxWindow.SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Disabled);
          RefreshClick();

       }

       private void RefreshClick()
        {
            var thumbs = new List<IntPtr>();
         _active.Clear();
         foreach (var window in _activeWindows)
          {
            var key = new WindowInteropHelper(window).Handle;
            thumbs.Add(key);
             if (!_active.ContainsKey(key))
             {
               _active.Add(key, window);
             }
          }

            this.DataContext = thumbs;
        }

       public Window ShowModalDialog()
       {
          var result = _modalBehavior.ShowModalDialog();
          if (result == ModalBehavior.ModalBehaviorStatus.Ok)
          {
            /*if (ListBoxWindow != null)
             {
                var selItems = ListBoxWindow.SelectedItems;
                if (selItems.Count > 0)
                {
                  var tt = (IntPtr)selItems[0];
                   if (_active.ContainsKey(tt))
                   {
                      return _active[tt];
                   }
                }
             }*/
          }
          return null;
       }
    }
+4
source share
3 answers

I install ItemsPresenter as follows

<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"  ClipToBounds="True" Width="400"/>

and now the result is the same

enter image description here

enter image description here

+2
source

The problem with your code is that you are overriding the control pattern but not using TemplateBindingfor ScrollViewer.HorizontalScrollBarVisibility, so the setting property ListBoxlike this does not work:

<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" ... /> 

, : 1) 2) ScrollViewer ( ).

:

<ListBox ... >
    <ListBox.Template>
        <ControlTemplate TargetType="ListBox">
            <Border ...>
                <ScrollViewer HorizontalScrollBarVisibility="Auto" ...>

Auto. Disabled

<ScrollViewer HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}" ...>
+1

, , , ClipToBounds:

https://msdn.microsoft.com/en-us/library/system.windows.uielement.cliptobounds(v=vs.110).aspx

ListBox TemplateBinding ItemsPresenter, ( , ItemPanelTemplate StackPanel...).

<ListBox Grid.Row="0"
         Name="ListBoxWindow" 
         ItemsSource="{Binding}" 
         ItemTemplate="{StaticResource Thumb}" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
         ScrollViewer.VerticalScrollBarVisibility="Disabled" 
         IsSynchronizedWithCurrentItem="True" 
         Margin="0,0,0,20" 
         Height="200" 
         Width="300"
         ClipToBounds="True">
    <ListBox.Template>
              <!--Does anyone know a better way to turn off horizontal scrolling in a ListBox?-->
        <ControlTemplate TargetType="ListBox"
                         xmlns:s="clr-namespace:System;assembly=mscorlib">
            <Border BorderBrush="{TemplateBinding Border.BorderBrush}"
                     BorderThickness="{TemplateBinding Border.BorderThickness}"
                     Name="Bd"
                     Background="{TemplateBinding Panel.Background}"
                     SnapsToDevicePixels="True"
                     Padding="1,1,1,1">
                <ScrollViewer VerticalScrollBarVisibility="{TemplateBinding ScrollViewer.VerticalScrollBarVisibility}"
                              HorizontalScrollBarVisibility="{TemplateBinding ScrollViewer.HorizontalScrollBarVisibility}"
                              Focusable="False"
                              Padding="{TemplateBinding Control.Padding}">
                       <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}"
                                       ClipToBounds="{TemplateBinding ClipToBounds}" />
                    </ScrollViewer>
                 </Border>
0

All Articles