4

There's an overflow in the listbow If someone have an idea of what I making wrong in this part of the code I check for this problem but not good answer

The ListView doesn't work too, I will try with an ItemControl, but it's not so easy to get the selected item

Maybe with a Zindex ? But doesn't work too

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>

The Thumbnail 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 Thumbnail UserControl

   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;
       }
    }
afaraut
  • 192
  • 12
  • Possible duplicate of [How can I disable horizontal scrolling in a WPF listbox?](http://stackoverflow.com/questions/373778/how-can-i-disable-horizontal-scrolling-in-a-wpf-listbox), assuming it's your question. – Sinatr Jun 17 '15 at 08:05
  • It doesn't work for me. I already try it .. `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" >` @Sinatr – afaraut Jun 17 '15 at 08:25
  • Yes, you're setting `HorizontalScrollBarVisibility` to `Disabled` there, but then inside the Template you're setting the ScrollViewer's `HorizontalScrollBarVisibility` to `Auto`. Set it to Disabled too, or use TemplateBindings as Sinatr suggests in his answer. – almulo Jun 17 '15 at 08:47

3 Answers3

2

I set the ItemsPresenter like this

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

and now the result is like this

enter image description here

enter image description here

afaraut
  • 192
  • 12
  • Weird... But I still don't know exactly what you're trying to achieve. Do you want the control to scroll or not? If not, how do you intend to show all those big items whitout scrolling? – almulo Jun 17 '15 at 09:25
  • Yes, I want to scroll .. I just don't want the content to go outside of the Usercontrol (container) I will set this post will the screen of what I want .. I want the red' section to be hidden – afaraut Jun 17 '15 at 09:34
  • Mmmh, the funny thing is that I just tried to reproduce your problem, and it works just fine for me from the start, without modifying your code at all... Except for changing your Thumbnails for red Rectangles. Can you add the `Thumbnail` Style/Template/UserControl XAML to your question? – almulo Jun 17 '15 at 09:46
  • Weird .. The Thumbnail represents an active window like aero style (Windows vista and more) .. I will edit the post Thank you very much for the time you spent on my post – afaraut Jun 17 '15 at 09:53
  • No problem! It might be an issue with rendering... Some not-WPF-native controls have problems rendering correctly inside a WPF application. – almulo Jun 17 '15 at 09:57
  • 1
    Ok thank you. I will maybe made a custom control (listBox) in order to remove this rendering problem due to the not-WPF-native control .. – afaraut Jun 17 '15 at 10:03
1

The problem in your code is what you redefine control template, but doesn't use TemplateBinding for ScrollViewer.HorizontalScrollBarVisibility, so that setting property of ListBox like this doesn't works:

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

Because you already have template, you can fix it by: 1) properly defining template binding 2) setting ScrollViewer property directly (inside your control template).

Here is mistake:

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

You set it to Auto. Set to Disabled or do like this

<ScrollViewer HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}" ...>
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • Thank you very much ! That fixes the overflow because the Parent UserControl takes the maximum width and reduce the size of each element in the listbox But I want to keep a width of 400 for the usercontrol (or the listBox) Thanks to WpfInspector I found that the "ItemsPresenter" has a width around 2500 .. That's the problem I think .. I'm on it to fix the problem @Sinatr – afaraut Jun 17 '15 at 09:11
  • The ItemsPresenter has a Width of 2500 because you're telling him to use an horizontal StackPanel to show your items (in your ItemsPanelTemplate). How do you want your items to appear then? – almulo Jun 17 '15 at 09:24
0

On the other hand, if you want to keep the horizontal scroll but prevent your items to overflow outside your ListBox, you might want to check the ClipToBounds property:

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

Set it in your ListBox and make a TemplateBinding to it in your ItemsPresenter, and it should stop the overflow (if not, try to set in your ItemsPanelTemplate's 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>
almulo
  • 4,918
  • 1
  • 20
  • 29
  • Doesn't work for me .. But Thank you very much `` – afaraut Jun 17 '15 at 09:07
  • I guess that'd be because you're overriding the Template. Try to set `ClipToBounds="True"` in your `ItemsPresenter` and/or in your ItemsPanelTemplate's `StackPanel`. – almulo Jun 17 '15 at 09:16