重新缩放列表框或其他项目?但不是文本 (WPF)

发布于 2024-11-07 15:40:13 字数 110 浏览 4 评论 0原文

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

我该怎么做?

谢谢

The question is quite clear. I have listviews or listboxes in my view.
If i make my screen smaller, i want that my text (labels etc) stay the same size. But the listviews and listboxes have to become smaller, eventually with a scrollbar?

how do i do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗心遗梦遗幸福 2024-11-14 15:40:13

ListBox 内置了一个 ScrollViewer。

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" />

the ListBox has a ScrollViewer built in.

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" />
雨后彩虹 2024-11-14 15:40:13

这个问题很清楚。

嗯,不完全是。但我认为这就是您正在寻找的。首先,创建一个接受 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 code> 将其中包含的任何单个元素保持为原始比例。因此,如果内容控件的比例加倍,则要保持不变的内容的比例将减半。

此示例显示了缩放内容控件和通过将 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>

The question is quite clear.

Well, not exactly. But I think this is what you're looking for. First, create a value converter that takes a Double and returns its reciprocal:

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();
    }
}

Now you can scale any content control, using a ScaleTransform, and use the ReciprocalValueConverter to keep any individual element contained within it to the original scale. So if the content control's scale is doubled, the content that you want to remain unchanged has its scale halved.

This example shows both scaling content controls and "unscaling" the items in a list box by assigning the LayoutTransform to each item:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文