简体   繁体   English

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

[英]Rescale listboxes or other items? but not text (WPF)

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

ListBox具有内置的ScrollViewer。

<ListBox ScrollViewer.VerticalScrollBarVisibility="Auto" />

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: 首先,创建一个使用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();
    }
}

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. 现在,您可以使用ScaleTransform缩放任何内容控件,并使用ReciprocalValueConverter将其中包含的任何单个元素保持为原始比例。 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: 此示例显示了缩放内容控件和通过将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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM