【问题标题】:Scrollviewer of a ListBox is Visible according to the count of ItemsListBox 的 Scrollviewer 根据项目数可见
【发布时间】:2019-07-29 01:52:14
【问题描述】:

我有一个定义了 DataTemplateListBoxListBoxItemSource (ListBoxItem) 是通过 ViewModel 提供的。

我希望在项目数超过 5 时显示 ListBox 的滚动查看器。如果有人可以帮助我,我会很高兴。

这是我的代码的一部分:

<ListBox  VerticalAlignment="Stretch" Grid.Column="0"   
                    ItemsSource="{Binding Path=Parts, Mode=OneWay}" SelectedIndex="{Binding CurrentPartIndex}"                       
                    Height="115" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowTextBrush}}">                   
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <Label Grid.Column="0" Content="{Binding CurrentLabel, Mode=OneWay}" MinWidth="150" Width="auto" VerticalAlignment="Stretch"/>
                <Label Grid.Column="1" Content="{Binding ItemNumber, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
                <Label Grid.Column="2" Content="{Binding Cut, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
                <Label Grid.Column="3" Content="{Binding Material, Mode=OneWay}" MinWidth="100" VerticalAlignment="Stretch"/>                           
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

【问题讨论】:

  • 尝试将水平和垂直 ScollbarVisibility 设置为自动。并玩弄控制高度,这样 5 个项目就不会导致滚动条出现。
  • 我会为 ScrollViewer 实现一个 IValueConverter,它将项目计数转换为 Horizo​​ntalalScrollBarVisibility,对于值 > 5 返回 Visible,否则为隐藏。
  • @PrateekShrivastava 我尝试过这种方式,但这不是一般的方式,因为它取决于分辨率。不同PC的变化

标签: c# wpf xaml mvvm listbox


【解决方案1】:

使用值转换器,以便 ListBox 中的项目数控制 ScrollViewer 的可见性。

转换器:

public class CountToVisibility : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if ((int)value > 5)
            return ScrollBarVisibility.Visible;
        else
            return ScrollBarVisibility.Hidden;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

xaml:

<Window.Resources>
    <local:CountToVisibility x:Key="ctv"/>
</Window.Resources>

...

<ListBox  VerticalAlignment="Stretch" Grid.Column="0"   
                ItemsSource="{Binding Path=Parts, Mode=OneWay}" SelectedIndex="{Binding CurrentPartIndex}"                       
                Height="115" BorderThickness="1" BorderBrush="{DynamicResource {x:Static SystemColors.WindowTextBrush}}"
ScrollViewer.HorizontalScrollBarVisibility="{Binding Path=Parts.Count,Converter={StaticResource ctv}}">                   
<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal" >
            <Label Grid.Column="0" Content="{Binding CurrentLabel, Mode=OneWay}" MinWidth="150" Width="auto" VerticalAlignment="Stretch"/>
            <Label Grid.Column="1" Content="{Binding ItemNumber, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
            <Label Grid.Column="2" Content="{Binding Cut, Mode=OneWay}" MinWidth="50" VerticalAlignment="Stretch"/>
            <Label Grid.Column="3" Content="{Binding Material, Mode=OneWay}" MinWidth="100" VerticalAlignment="Stretch"/>                           
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

【讨论】:

    猜你喜欢
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 2011-08-30
    • 2018-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多