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

时间:2015-06-17 07:37:35

标签: c# .net wpf xaml

  

listbow有一个溢出   如果有人知道我在代码的这一部分做错了什么   我检查了这个问题但不是很好的答案

     

ListView也不起作用,   我将尝试使用ItemControl,但获取所选项目并不容易

     

也许使用Zindex?但是也不起作用

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>
  

缩略图样式

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

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

3 个答案:

答案 0 :(得分:2)

  

我像这样设置ItemsPresenter

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

现在结果就像这样

enter image description here

enter image description here

答案 1 :(得分:1)

您的代码中的问题是您重新定义控件模板的问题,但TemplateBinding不使用ScrollViewer.HorizontalScrollBarVisibility,因此ListBox的设置属性不起作用:< / p>

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

因为您已经有模板,所以可以通过以下方式修复它:1)正确定义模板绑定2)直接设置ScrollViewer属性(在控件模板中)。

这是错误的:

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

您将其设为Auto。设置为Disabled或类似

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

答案 2 :(得分:0)

另一方面,如果你想保持水平滚动但是防止你的项目溢出ListBox,你可能想要检查 ClipToBounds 属性:

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

在ListBox中设置它并在ItemsPresenter中为它创建一个TemplateBinding,它应该停止溢出(如果没有,尝试在ItemsPanelTemplate的StackPanel中设置......)。

{{1}}