我有一个自定义样式与模板的树视图项目。我希望项目演示器显示内容的宽度和高度的网格行,它是在和离开滚动到子控件。这工作…排序的。子进程确实执行滚动,但由于某种原因,高度被设置为父进程的高度(树视图),这就搞砸了滚动。我已经复制了下面的代码。
<Style TargetType="TreeViewItem">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Margin" Value="0"/>
<Setter Property="Padding" Value="0"/>
<Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="1,0,0,0"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="FocusVisualStyle" Value="{StaticResource TreeViewItemFocusVisual}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TreeViewItem">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Border Name="Bd" Background="LightGray" BorderBrush="Black" Grid.Row="0"
BorderThickness="1" Padding="{TemplateBinding Padding}">
<Grid Margin="5,0,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Path x:Name="ExpandPath" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="1,1,1,1"
Fill="{StaticResource GlyphBrush}" Data="M 0 4 L 16 4 L 8 12 Z" Grid.Column="0"/>
<Label Grid.Column="1" x:Name="PART_Header" Content="{TemplateBinding Header}" FontSize="18" FontWeight="Bold"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"/>
</Grid>
</Border>
<ItemsPresenter Name="ItemsHost" Grid.Row="1"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsExpanded" Value="false">
<Setter TargetName="ItemsHost" Property="Visibility" Value="Collapsed"/>
<Setter Property="Data" TargetName="ExpandPath" Value="M 8 0 L 16 8 L 8 16 Z"/>
</Trigger>
<Trigger Property="HasItems" Value="false">
<Setter TargetName="ExpandPath" Property="Visibility" Value="Collapsed"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader" Value="false"/>
<Condition Property="Width" Value="Auto"/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header" Property="MinWidth" Value="75"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="HasHeader" Value="false"/>
<Condition Property="Height" Value="Auto"/>
</MultiTrigger.Conditions>
<Setter TargetName="PART_Header" Property="MinHeight" Value="19"/>
</MultiTrigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="IsExpanded" Value="True"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="IsExpanded" Value="False"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Resources>
<Style TargetType="{x:Type TreeViewItem}">
</Style>
</Style.Resources>
</Style>
我想出了一个在c#中做到这一点的方法,当选择改变时设置子元素的宽度和高度。