我有一个自定义样式,其中包含树视图项的模板。我希望项目演示者在其所在的网格行的宽度和高度中显示内容,并向上滚动到子控件。这有点......有点儿。孩子确实做了滚动,但高度由于某种原因设置为父亲的高度(树视图),这会使滚动变得混乱。我复制了下面的代码。
<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>
答案 0 :(得分:0)
通过在选择更改时设置子元素的宽度和高度,我想出了一种在C#中执行此操作的方法。