<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!-- … -->
</Grid.RowDefinitions>
<TextBlock Grid.Column="0">
This should be allways visible, even if the expander isn’t expanded!
</TextBlock>
<Expander ExpandDirection="Left" Grid.Column="1">
<Expander.Header>
<!-- … -->
</Expander.Header>
<TreeView MinWidth="50"/>
</Expander>
<!-- … -->
</Grid>
我希望用户能够调整TreeView
的大小。我尝试在第一列中的TreeView
{2}和Grid
中扭曲GridSplitter
,但这不起作用。有没有人知道如何做到这一点?
P.S。:仅限XAML的答案会很棒。
答案 0 :(得分:5)
您可以使用Expander.Collapsed和Expander.Expanded事件作为附加事件来解决您的问题。我现在还没有关于仅使用Xaml的想法,但以下代码在我的情况下运行良好。
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow">
<Grid Expander.Collapsed="Grid_Collapsed" Expander.Expanded="Grid_Expanded">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<!-- … -->
</Grid.RowDefinitions>
<TextBlock Grid.Column="0" TextWrapping="Wrap">
This should be allways visible, even if the expander isn’t expanded!
</TextBlock>
<GridSplitter HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="2" />
<Expander Background="Yellow" ExpandDirection="Left" Grid.Column="1">
<Expander.Header>test</Expander.Header>
<TreeView MinWidth="50"/>
</Expander>
<!-- … -->
</Grid>
</Window>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private GridLength _rememberWidth = GridLength.Auto;
private void Grid_Collapsed(object sender, RoutedEventArgs e)
{
var grid = sender as Grid;
if(grid != null)
{
_rememberWidth = grid.ColumnDefinitions[1].Width;
grid.ColumnDefinitions[1].Width = GridLength.Auto;
}
}
private void Grid_Expanded(object sender, RoutedEventArgs e)
{
var grid = sender as Grid;
if (grid != null)
{
grid.ColumnDefinitions[1].Width = _rememberWidth;
}
}
}
答案 1 :(得分:0)
您只需要在包装网格中添加另一列,以使其正常工作。
这是一个适合我的XAML示例:
<Grid x:Name="LayoutRoot">
<toolkit:Expander ExpandDirection="Left" Header="ImLeftExpandedExpander">
<Grid ShowGridLines="True" Background="White" >
<Grid.RowDefinitions>
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="50" />
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<sdk:TreeView Background="BurlyWood">
<sdk:TreeViewItem Header="Root">
<sdk:TreeViewItem Header="bla1"/>
<sdk:TreeViewItem Header="bla2"/>
<sdk:TreeViewItem Header="bla3"/>
</sdk:TreeViewItem>
</sdk:TreeView>
<sdk:GridSplitter x:Name="grsplSplitter" Grid.Row="0" Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Center" Background="Red" Width="5"></sdk:GridSplitter>
<Grid Background="#CCCC66" Grid.Column="2">
<TextBlock FontSize="22" Text="This column can be left empty, its just so the GridSplitter will have space to expand to" TextWrapping="Wrap"/>
</Grid>
</Grid>
</toolkit:Expander>
</Grid>
结果:
答案 2 :(得分:0)
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TreeView>
<TreeViewItem Header="1">
<TreeViewItem Header="2">
<TreeViewItem Header="3"/>
</TreeViewItem>
<TreeViewItem Header="3">
<TreeViewItem Header="4"/>
</TreeViewItem>
<TreeViewItem Header="5"/>
<TreeViewItem Header="6"/>
</TreeViewItem>
</TreeView>
<GridSplitter Grid.Column="1" Width="10" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"/>
<Canvas Grid.Column="2" Background="LightGray"/>
</Grid>
编辑:这是一个工作示例,显示了这两种方法。如果这不是你想要的,请说出来。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication1.MainWindow"
Title="MainWindow" d:DesignWidth="516" d:DesignHeight="310">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Label Content="Change size of content"/>
<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Margin="10">
<Expander Header="Expander" >
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<TreeView>
<TreeViewItem Header="1">
<TreeViewItem Header="2">
<TreeViewItem Header="3"/>
</TreeViewItem>
<TreeViewItem Header="3">
<TreeViewItem Header="4"/>
</TreeViewItem>
<TreeViewItem Header="5"/>
<TreeViewItem Header="6"/>
</TreeViewItem>
</TreeView>
<GridSplitter Grid.Column="1" Width="10" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"/>
<Canvas Grid.Column="2" Background="LightGray"/>
</Grid>
</Expander>
</Border>
<Label Content="Change size of expander" Grid.Column="1"/>
<Border BorderBrush="Black" BorderThickness="1" Grid.Row="1" Grid.Column="1" Margin="10">
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Expander>
<TreeView>
<TreeViewItem Header="1">
<TreeViewItem Header="2">
<TreeViewItem Header="3"/>
</TreeViewItem>
<TreeViewItem Header="3">
<TreeViewItem Header="4"/>
</TreeViewItem>
<TreeViewItem Header="5"/>
<TreeViewItem Header="6"/>
</TreeViewItem>
</TreeView>
</Expander>
<GridSplitter Grid.Column="1" Width="10" ResizeDirection="Columns" ResizeBehavior="PreviousAndNext"/>
<Canvas Grid.Column="2" Background="LightGray"/>
</Grid>
</Border>
</Grid>
</Window>