これは、この古い投稿をいくつかのビジュアルで拡張するためのものです。
問題文: デフォルトで TreeView のコンテンツを右揃えにすることはできません - こことそこの他のコメントに基づいて - Xaml からスタックパネルを削除することをテストしました (ただし、スタックパネルを使用して TreeView の外部で目的のデザインを複製できるため、問題ではありません)。
TLDR: 回避策は以前のコメントに投稿されています
XAML:
<Window x:Class="Test"
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"
xmlns:local="clr-namespace:DbSeeder.WPF.View"
mc:Ignorable="d"
Title="Test"
Height="450"
Width="800"
Loaded="Window_Loaded">
<DockPanel>
<!-- what i want -->
<StackPanel DockPanel.Dock="Top"
Margin="5">
<Label Content="This is the expected structure"
FontFamily="Verdana"
FontWeight="Bold"
BorderBrush="Black"
BorderThickness="2"
HorizontalAlignment="Stretch"
Background="Orange"
/>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="5*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Buttons -->
<StackPanel Grid.Column="1"
Orientation="Horizontal"
HorizontalAlignment="Right"
Margin="3">
<Button Content="Button 1"
Margin="5"/>
<Button Content="Button 2"
Margin="3"/>
</StackPanel>
<!-- Keys -->
<StackPanel Grid.Column="0"
Orientation="Horizontal"
HorizontalAlignment="Left"
Margin="3" >
<Label Content="Here should come key 1" Margin="3" BorderBrush="Black" BorderThickness="2"/>
<Label Content="Here should come key 2" Margin="3" BorderBrush="Black" BorderThickness="2"/>
</StackPanel>
</Grid>
</StackPanel>
<!-- What I get 2-->
<Grid
ShowGridLines="True"
DockPanel.Dock="Top"
Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="This is what I get"
FontFamily="Verdana"
FontWeight="Bold"
BorderBrush="Black"
BorderThickness="2"
Background="Orange"
Grid.Column="0"
Grid.Row="0"/>
<TreeView x:Name="alternative2"
Grid.Column="0"
Grid.Row="1"
HorizontalContentAlignment="Stretch"
HorizontalAlignment="Stretch">
<TreeView.Resources>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<Grid ShowGridLines="True"
DockPanel.Dock="Top"
Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="3*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Buttons -->
<Button Content="Button 1"
Grid.Column="2"
Margin="5"
HorizontalAlignment="Stretch"/>
<Button Content="Button 2"
Grid.Column="3"
Margin="3"
HorizontalAlignment="Stretch"/>
<!-- Keys -->
<Label Grid.Column="0"
HorizontalAlignment="Stretch"
Content="Here should come key 1"
Margin="3"
BorderBrush="Black"
BorderThickness="2"/>
<Label Grid.Column="1"
HorizontalAlignment="Stretch"
Content="Here should come key 2"
Margin="3"
BorderBrush="Black"
BorderThickness="2"/>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TreeView.Resources>
</TreeView>
</Grid>
</DockPanel>
そして背後にあるコード(ビューをシードするため)
public partial class Test : Window
{
protected IDictionary<string, object> JsonFields { get; set; }
public Test()
{
InitializeComponent();
var x = new Dictionary<string, string>()
{
{"key 1", "string" },
{"key 2", "string 2" }
};
var y = new List<string>()
{
"subList5a",
"subList5b",
};
JsonFields = new Dictionary<string, object>()
{
{ "Key 1", "string" },
{ "Key 2", "string" },
{ "Key 3", "string" },
{ "Key 4 - dict", x },
{ "Key 5 - List", y }
};
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var counter = 0;
foreach (var key in JsonFields.Keys)
{
counter++;
var item = new TreeViewItem()
{
Header = key,
ClipToBounds = true
};
if (counter % 2 == 0)
{
var subItem = new TreeViewItem()
{
Header = $"SubKey of {item.Header}",
ClipToBounds = true
};
item.Items.Add(subItem);
}
alternative2.Items.Add(item);
}
}
}