[英]Horizontal to vertical WrapPanel in WPF
我嘗試使用垂直按鈕構建包裝面板。 每個按鈕由圖像和文本塊組成。 當用戶移動 GridSplitter 時,我想做一些 Microsoft 在 window 左側的 Outlook 中所做的事情。 當用戶將減少環繞面板的高度時,如果任何按鈕沒有位置,文本塊將消失(按鈕將只包含圖像)。
我怎樣才能做到這一點。
謝謝
如果我理解正確的話。 你想顯示帶有文本和圖像的按鈕,但如果按鈕的寬度減小到一定大小,它只會顯示圖像。
如果是這樣,您應該能夠使用數據觸發器來實現。
<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>
還使用這個 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();
}
}
我不是這方面的專家,所以可能有更簡潔的方法。 我還使用了列表視圖而不是您請求的包裝面板。
希望這可以幫助
聽起來您想使用的是Expander
控件。 這篇StackOverflow 帖子解釋了如何讓其他Expander
在您打開另一個時自動關閉。 這將像您在 Outlook 中描述的那樣工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.