RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 553951
Accepted
Lightness
Lightness
Asked:2020-08-10 19:16:27 +0000 UTC2020-08-10 19:16:27 +0000 UTC 2020-08-10 19:16:27 +0000 UTC

列表框视觉状态

  • 772

我怎样才能做到这一点ListBox,根据窗口的大小,我可以决定向它显示多少行信息?

假设有这个xaml:

<ListBox>
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid IsItemsHost="True" Rows="2"></UniformGrid>
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
</ListBox>

如果窗口的高度500px要以行UniformGrid显示2,如果更多,700px则以3. 如何实施?

PS我听说过一些事情VisualState,在我看来它们可以用于这种情况,但我没有找到一个正常的例子。

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    VladD
    2020-08-10T21:16:01Z2020-08-10T21:16:01Z

    如果没有代码隐藏,它可能根本无法工作。

    一个简单的解决方案是通过转换器计算将窗口高度转换为行数的逻辑:

    class WindowHeightToRowsConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            var height = (double)value;
            if (height < 500.0)
                return 1;
            else if (height < 700)
                return 2;
            else
                return 3;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    有了这个你可以做一个简单的绑定:

    <Window x:Class="..." ...
            Width="525" Name="Root">
        <Window.Resources>
            <local:WindowHeightToRowsConverter x:Key="H2R"/>
        </Window.Resources>
        <Grid>
            <ListBox ItemsSource="...">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <UniformGrid
                            IsItemsHost="True"
                            Rows="{Binding ActualHeight, ElementName=Root,
                                     Converter={StaticResource H2R}}"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </Grid>
    </Window>
    

    这种解决方案的缺点是类逻辑WindowHeightToRowsConverter非常具体,难以重用。一个更复杂但可能可重用的解决方案,例如这个。

    我们把比较转换器:

    class LessThanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            return (double)value < (double)parameter;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    
    class GreaterOrEqConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter,
                              CultureInfo culture)
        {
            return (double)value >= (double)parameter;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter,
                                  CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    现在,为了方便在代码中指定常量,这里是以下内容:

    public class DoubleExtension : MarkupExtension
    {
        public DoubleExtension(double value) { Value = value; }
        public double Value { get; set; }
        public override object ProvideValue(IServiceProvider sp) { return Value; }
    }
    

    我们编写以下 XAML:

    <Window x:Class="..."
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            xmlns:local="..."
            ...
            Name="Root">
        <Window.Resources>
            <local:LessThanConverter x:Key="LT"/>
            <local:GreaterOrEqConverter x:Key="GE"/>
        </Window.Resources>
    
    <UniformGrid IsItemsHost="True">
        <UniformGrid.Style>
            <Style TargetType="UniformGrid">
                <Setter Property="Rows" Value="1"/>
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <!-- если высота >= 500... -->
                            <Condition Binding="{Binding ActualHeight, ElementName=Root,
                                                   Converter={StaticResource GE},
                                                   ConverterParameter={local:Double 500}}"
                                       Value="True"/>
                            <!-- и < 700... -->
                            <Condition Binding="{Binding ActualHeight, ElementName=Root,
                                                   Converter={StaticResource LT},
                                                   ConverterParameter={local:Double 700}}"
                                       Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <!-- то Rows = 2 -->
                        <Setter Property="Rows" Value="2"/>
                    </MultiDataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <!-- если высота >= 700... -->
                            <Condition Binding="{Binding ActualHeight, ElementName=Root,
                                                   Converter={StaticResource GE},
                                                   ConverterParameter={local:Double 700}}"
                                       Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <!-- то Rows = 3 -->
                        <Setter Property="Rows" Value="3"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </UniformGrid.Style>
    </UniformGrid>
    

    这样就可以把简单的条件组合成复杂的条件。

    (local:Double工作谢谢DoubleExtension。)

    • 2

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5