简体   繁体   中英

Horizontal to vertical WrapPanel in WPF

I try build wrap panel with vertical buttons. Every button consist from image and textblock. I want to do something that Microsoft did in the Outlook on the left side of the window, when the user move the GridSplitter. When the user will reduce the height of the wrap panel, if any button will not has place, the textblock will disappear (the button will consist just from image).

How can I do that.

Thank

If I understand correctly. You want to show buttons with Text and an Image, but if the width of the button is reduced to a certain size, it will only show the image.

If so, you should be able to implement with a 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>

Also using this IValueConvertor:

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

}

I am not an expert at this, so there may be a cleaner way. I also used a listview instead of your requested wrappanel.

Hope this helps

It sounds like what you want to use is an Expander control. This StackOverflow post explains how to make the other Expander s close automatically when you open another. This will work just like what you're describing in Outlook.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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