我写了一个UserControl
,其中包含两个级别的Expander
。 Expander.Header
不均匀地分布嵌套的Grid / StackPanel / Docpanel。如何在这种结构中对齐网格上的元素?
我的用户控件xaml:
<UserControl x:Class="UserInterface.UserStructuresControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserInterface"
xmlns:res="clr-namespace:MigrationTool.Localization;assembly=MigrationTool.Localization"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<CollectionViewSource
x:Key="TableSource"
Source="{Binding Path=TablesCollection}">
</CollectionViewSource>
<DataTemplate x:Key="ExpandableRow">
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="20,0,0,0"/>
</Style>
</StackPanel.Resources>
<CheckBox></CheckBox>
<TextBlock Text="{Binding Path=TableName}"></TextBlock>
<TextBlock HorizontalAlignment="Right" Text="{Binding Path=TableDescription}"></TextBlock>
</StackPanel>
</Expander.Header>
<StackPanel>
<DataGrid ItemsSource="{Binding Path=FieldCollection}"
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False"
>
<DataGrid.Columns>
<DataGridTemplateColumn
Header="{x:Static res:LocResources.UserFieldsHeader}"
Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander>
<Expander.Header>
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="20,0,0,0"/>
</Style>
</StackPanel.Resources>
<CheckBox></CheckBox>
<TextBlock Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Description}"/>
</StackPanel>
</Expander.Header>
<Grid>
<TextBlock Text="{Binding Path=Description}"></TextBlock>
</Grid>
</Expander>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</StackPanel>
</Expander>
</DataTemplate>
</UserControl.Resources>
<Grid>
<DataGrid
ItemsSource="{Binding Source={StaticResource ResourceKey=TableSource}}"
CanUserAddRows="False"
CanUserDeleteRows="False"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn
SortMemberPath="TableName"
CanUserSort="True"
Header="{x:Static res:LocResources.TableNameHeader}"
CellTemplate="{StaticResource ExpandableRow}"
Width="*">
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
我还想问:如果在滚动鼠标滚轮时光标进入了嵌套Expander的开放区域,则外部ScrollBar不再处于活动状态。如何仅在外部ScrollBar上设置滚轮?
答案 0 :(得分:1)
在您的模板中,将堆栈面板替换为具有列的网格:
<StackPanel Orientation="Horizontal">
<StackPanel.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="20,0,0,0"/>
</Style>
</StackPanel.Resources>
<CheckBox></CheckBox>
<TextBlock Text="{Binding Path=Name}"/>
<TextBlock Text="{Binding Path=Description}"/>
</StackPanel>
与此:
<Grid>
<Grid.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="20,0,0,0"/>
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0"></CheckBox>
<TextBlock Grid.Column="1" Text="{Binding Path=Name}"/>
<TextBlock Grid.Column="2" Text="{Binding Path=Description}"/>
</Grid>