【问题标题】:Rescale listboxes or other items? but not text (WPF)重新缩放列表框或其他项目?但不是文本(WPF)
【发布时间】:2011-05-16 11:22:22
【问题描述】:

这个问题很清楚。我的视图中有列表视图或列表框。 如果我让我的屏幕更小,我希望我的文本(标签等)保持相同的大小。但是列表视图和列表框必须变小,最终带有滚动条?

我该怎么做?

谢谢

【问题讨论】:

    标签: wpf list rescale


    【解决方案1】:

    ListBox 内置了一个 ScrollViewer。

    <ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" />
    

    【讨论】:

    • 这不是重点,他想要的是将列表框重新缩放到足够小以显示滚动条。
    • 列表框内的内容必须比列表框本身更大(例如固定大小)。我会说-1,因为这个问题误导了我;)
    • 您也可以将列表框放入滚动查看器并滚动整个列表框。有很多机会可以滚动内容。
    【解决方案2】:

    这个问题很清楚。

    嗯,不完全是。但我认为这就是你要找的。首先,创建一个接受Double 并返回其倒数的值转换器:

    public class ReciprocalValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Double? d = value as Double?;
            return (d == null || d == 0)
                       ? null
                       : 1/d;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    现在您可以使用ScaleTransform 缩放任何内容控件,并使用ReciprocalValueConverter 将其中包含的任何单个元素保持为原始比例。因此,如果内容控件的比例加倍,则您希望保持不变的内容的比例减半。

    此示例通过将LayoutTransform 分配给每个项目来显示缩放内容控件和“取消缩放”列表框中的项目:

    <Window x:Class="ScaleTransformDemo.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ScaleTransformDemo="clr-namespace:ScaleTransformDemo" Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <ScaleTransformDemo:ReciprocalValueConverter x:Key="Reciprocal" />
        </Window.Resources>
        <DockPanel>
            <Slider x:Name="ScaleSlider"
                    Orientation="Vertical"
                    Minimum=".2"
                    Maximum="4"
                    Value="1" />
        <DockPanel>
            <DockPanel.LayoutTransform>
                <ScaleTransform ScaleX="{Binding ElementName=ScaleSlider, Path=Value}"
                                ScaleY="{Binding ElementName=ScaleSlider, Path=Value}" />
            </DockPanel.LayoutTransform>
            <Label DockPanel.Dock="Top">
                The content of this label scales with the slider.
            </Label>
            <Label DockPanel.Dock="Top">
                <Label.LayoutTransform>
                    <ScaleTransform ScaleX="{Binding ElementName=ScaleSlider, Path=Value, Converter={StaticResource Reciprocal}}"
                                    ScaleY="{Binding ElementName=ScaleSlider, Path=Value, Converter={StaticResource Reciprocal}}" />
                </Label.LayoutTransform>
                <Label.Content>
                    This label's content stays the same size.
                </Label.Content>
            </Label>
            <Label DockPanel.Dock="Top">
                The ListBox below scales with the slider, too, but the ListBoxItems don't:
            </Label>
            <ListBox Height="50"
                     DockPanel.Dock="Top">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="LayoutTransform">
                            <Setter.Value>
                                <ScaleTransform ScaleX="{Binding ElementName=ScaleSlider, Path=Value, Converter={StaticResource Reciprocal}}"
                                                ScaleY="{Binding ElementName=ScaleSlider, Path=Value, Converter={StaticResource Reciprocal}}" />
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
                <ListBoxItem>Item 1</ListBoxItem>
                <ListBoxItem>Item 2</ListBoxItem>
                <ListBoxItem>Item 3</ListBoxItem>
                <ListBoxItem>Item 4</ListBoxItem>
                <ListBoxItem>Item 5</ListBoxItem>
                <ListBoxItem>Item 6</ListBoxItem>
            </ListBox>
            <TextBlock DockPanel.Dock="Top" />
        </DockPanel>
    </DockPanel>
    </Window>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-21
      • 2013-04-07
      • 2020-06-27
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      相关资源
      最近更新 更多