Как убрать отступ у дочерних елементов TreeViewItem?
Щелкните по TreeView в дизайнере правой кнопкой и выберите Edit Additional Templates>Edit Generated Item Container (ItemContainerStyle)>Edit a Copy... введите имя стиля и нажмите OK
Ищем в полученных ресурсах стиль для TreeViewItem (<Style x:Key="TreeViewItemStyle1" TargetType="{x:Type TreeViewItem}">) и в нем шаблон (<Setter Property="Template">), видим, что в шаблоне Grid с несколькими колонками и ItemsPresenter (который предназначен для вывода дочерних элементов) лежит во второй-третьей колонках:
<ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1"/>
меняем:
<ItemsPresenter x:Name="ItemsHost" Grid.ColumnSpan="3" Grid.Column="0" Grid.Row="1"/>
получаем:
По сути здесь просто нужен вложенный ItemsControl или что то на его основе.
К примеру используем такой вид:
<ItemsControl ItemsSource="{Binding TestItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Expander Header="{Binding Name}">
<ItemsControl ItemsSource="{Binding Items}" BorderThickness="0">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Expander>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
TestItems - это простая коллекция ObservableCollection<Test>.
Test - класс, который содержит свойство с именем и некий внутренний массив:
class Test
{
public string Name { get; set; }
public string[] Items { get; set; }
}
Результат получим такой:
Конечно вы можете тут настроит все как вам нужно. К примеру если хотите выделение, то можно ItemsControl переделать на ListBox со своим стилем или чем то еще.
Также тут можно пойти по пути группировки, то есть наши элементы к примеру будут такого вида:
class Test
{
public string Group { get; set; }
public string Name { get; set; }
}
Тогда View с группировкой будет следующий:
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="TestGroups" Source="{Binding TestItems}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="Group" />
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
</Grid.Resources>
<ItemsControl ItemsSource="{Binding Source={StaticResource TestGroups}}">
<ItemsControl.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</ItemsControl.GroupStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
Результат получим такой:
В общем все зависит от конечного результата, я показал только как можно поступить. Удачи!
Заслуга идеи @EvgeniyZ: нужно выбирать более подходящий контрол
<ListBox ItemsSouce="{Binding }">
<ListBox.ItemsTemplate>
<DataTempalte>
<Expander/>
</DataTemplate>
</ListBox.ItemsTemplate>
</ListBox>
Сборка персонального компьютера от Artline: умный выбор для современных пользователей