简体   繁体   English

在列表框中有一个项目溢出

[英]Have an items overflow in a Listbox

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 listbow中有一个溢出如果有人知道我在代码的这一部分出错了什么我检查这个问题但不是很好的答案

The ListView doesn't work too, I will try with an ItemControl, but it's not so easy to get the selected item ListView也不起作用,我将尝试使用ItemControl,但获取所选项目并不容易

Maybe with a Zindex ? 也许用Zindex? But doesn't work too 但是也行不通

在此输入图像描述

 <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 这是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;
       }
    }

I set the ItemsPresenter like this 我像这样设置ItemsPresenter

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

and now the result is like this 现在结果是这样的

在此输入图像描述

在此输入图像描述

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: 您的代码中的问题是您重新定义控件模板,但不使用TemplateBinding ScrollViewer.HorizontalScrollBarVisibility ,因此像这样的ListBox设置属性不起作用:

<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). 因为您已经有模板,所以可以通过以下方式修复它:1)正确定义模板绑定2)直接设置ScrollViewer属性(在控件模板内)。

Here is mistake: 这是错误的:

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

You set it to Auto . 您将其设置为Auto Set to Disabled or do like this 设置为Disabled或执行此操作

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

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: 另一方面,如果要保持水平滚动但是防止项目溢出ListBox,您可能需要检查ClipToBounds属性:

https://msdn.microsoft.com/en-us/library/system.windows.uielement.cliptobounds(v=vs.110).aspx 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中设置它并在ItemsPresenter中为它创建一个TemplateBinding,它应该停止溢出(如果没有,尝试在ItemsPanelTemplate的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM