WPF中的水平到垂直包裹面板

本文关键字:包裹 垂直 水平 WPF | 更新日期: 2023-09-27 18:30:03

我尝试构建带有垂直按钮的包装面板。每个按钮由图像和文本块组成。我想做一些微软在窗口左侧的Outlook中做的事情,当用户移动GridSplitter时。当用户将减小包裹面板的高度时,如果任何按钮都没有位置,文本块将消失(按钮将仅由图像组成)。

我该怎么做。

感谢

WPF中的水平到垂直包裹面板

如果我理解正确。您希望显示带有"文本"answers"图像"的按钮,但如果按钮的宽度减小到一定大小,则只显示图像。

如果是这样,您应该能够使用datatrigger来实现。

<Window x:Class="StackOverflow1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:StackOverflow1"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <local:isLessThanConverter x:Key="MyisLessThanConverter"/>
    <Style x:Key="myWidthTrigger" TargetType="TextBlock">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Button}, Converter={StaticResource MyisLessThanConverter}, ConverterParameter=90}" Value="True">
                    <Setter Property="Visibility" Value="Collapsed" />
                </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"></ColumnDefinition>
        <ColumnDefinition Width="Auto"></ColumnDefinition>
        <ColumnDefinition Width="*"></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ListView HorizontalContentAlignment="Stretch">
        <Button x:Name="myButton" Width="Auto">
            <StackPanel Orientation="Horizontal">
                <TextBlock x:Name="MyTextBlock" Style="{StaticResource myWidthTrigger}" Text="Test"></TextBlock>
                <Image Source="image.png" Height="15"></Image>
            </StackPanel>
        </Button>
    </ListView>
    <GridSplitter Width="5" Grid.Column="1" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"></GridSplitter>
</Grid>

也使用此IV值转换器:

[ValueConversion(typeof(double), typeof(string))]
public class isLessThanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
            if ((double)value < double.Parse((string)parameter))
            {
                return true;
            }
            else
            {
                return false;
            }
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

我不是这方面的专家,所以可能有一个更干净的方法。我还使用了列表视图,而不是您要求的包装面板。

希望这能帮助

听起来您想要使用的是Expander控件。这篇StackOverflow文章解释了如何在打开另一个Expander时使其自动关闭。这将像您在Outlook中描述的一样工作。