【问题标题】:Hierarchy as grid层次结构作为网格
【发布时间】:2023-03-21 04:46:01
【问题描述】:

我有层次结构:

public class Parameter
{
    public string Name { get; set; }
    public Value Value { get; set; }
}

public abstract class Value
{
}

public class StringValue : Value
{
    public string Str { get; set; }
}

public class ComplexValue : Value
{
    public ComplexValue()
    {
        Parameters = new List<Parameter>();
    }

    public List<Parameter> Parameters { get; set; }
}

/// Contains ComplexValue
public class ComplexParameter : Parameter
{
}

以及带有模板的 XAML

<Window.Resources>
    <DataTemplate DataType="{x:Type pc:Parameter}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>
            <Label Grid.Column="0" Content="{Binding Name}"/>
            <ContentPresenter Grid.Column="1" Content="{Binding Value}"/>
        </Grid>
    </DataTemplate>

    <DataTemplate DataType="{x:Type pc:ComplexParameter}">
        <StackPanel>
            <Label Content="{Binding Name}"/>
            <ContentControl Margin="18,0,0,0" Content="{Binding Value}"/>
        </StackPanel>
    </DataTemplate>

    <DataTemplate DataType="{x:Type pc:ComplexValue}">
        <ItemsControl ItemsSource="{Binding Parameters}"/>
    </DataTemplate>

    <DataTemplate DataType="{x:Type pc:StringValue}">
        <TextBox Text="{Binding Str}"/>
    </DataTemplate>

</Window.Resources>

这看起来像:

Param1         -Control----
Param2         -Control----
Complex1
  Sub Param1     -Control-
  Sub Param2     -Control-

或图片在这里:freeimagehosting.net/uploads/9d438f52e7.png

问题

如何仅在左列(参数名称)中进行缩进。 像这样的:

Param1         -Control----
Param2         -Control----
Complex1
  Sub Param1   -Control----
  Sub Param2   -Control----

或图片在这里:freeimagehosting.net/uploads/4ab3045b75.png

抱歉,我无法添加网址和图片 :(

【问题讨论】:

    标签: c# wpf xaml mvvm hierarchy


    【解决方案1】:

    据我了解,此任务没有简单的决定,因为视图模型应提供有关层次结构中节点级别的信息。如果为视图模型提供级别,我可以使用this decision

    也很有趣this example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-16
      • 1970-01-01
      • 2019-05-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多