TreeView WPF绑定层次结构

时间:2013-09-11 10:14:28

标签: wpf templates treeview

我的问题是我不知道如何从CModul绑定两个List的CModul_I_O(输入,输出)。 我用HierarchicalDataTemplate和datatemplates尝试了很多方法,但我总是只能绑定这2个列表中的一个。它还仅显示第一个CModul的列表。

我需要一个看起来像这样的TreeView

    -Modul1
        -Inputs
            input1
            input2
            input3
        -outputs
            output1
            output2
            output3
    -Modul2
        -Inputs
            input1
            input2
            input3
        -outputs
            output1
            output2
            output3

我的2个数据类

public class CModul
{
    protected string sName, sKlasse;
    protected List<CModul_I_O> lInputs,lOutputs;

    public CModul()
    {
        sName = "";
        sKlasse = "";
        lInputs = new List<CModul_I_O>();
        lOutputs = new List<CModul_I_O>();
    }

    public string Name
    {
        get { return sName; }
        set { sName = value; }
    }
    ...

    public List<CModul_I_O> Inputs
    {
        get { return lInputs; }
        set { lInputs = value; }
    }
    public List<CModul_I_O> Outputs
    {
        get { return lOutputs; }
        set { lOutputs = value; }
    }
}

 public class CModul_I_O
{
    string sName, sVar, sKommentar;

    public CModul_I_O()
    {
        sName="";
        sVar="";
        sKommentar="";
    }

    public string Name
    {
        get { return sName; }
        set { sName = value; }
    }
    ....
 }

 List<CModul> lModule = new List<CModul>();

我有xaml代码

 <Window.Resources>
 <DataTemplate x:Key="module"  DataType="{x:Type local:CModul_I_O}">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition Width="150"></ColumnDefinition>
                <ColumnDefinition Width="150"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <TextBox Grid.Column="0" Margin="5" Text="{Binding Path=Name}"></TextBox>
            <TextBox Grid.Column="1" Margin="5" Text="{Binding Path=Kommentar}"></TextBox>
            <TextBox Grid.Column="1" Margin="5" Text="{Binding Path=Var}"></TextBox>
        </Grid>
    </DataTemplate>

</Window.Resources>
<Grid>
    <TreeView Name="TV_ModulListe" Grid.Row="0">
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:CModul}"  ItemsSource="{Binding Inputs}" ItemTemplate="{StaticResource  module}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="150"></ColumnDefinition>
                        <ColumnDefinition Width="150"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBox Grid.Column="0" Margin="5" Text="{Binding Path=Name}"></TextBox>
                    <TextBox Grid.Column="1" Margin="5" Text="{Binding Path=Klasse}"></TextBox>
                </Grid>
            </HierarchicalDataTemplate> 
        </TreeView.Resources>
    </TreeView>
</Grid>

1 个答案:

答案 0 :(得分:0)

这显示出类似的内容?

-Modul1
    -Inputs
        input1
        input2
        input3
-Modul2
    -Inputs
        input1
        input2
        input3

你应该有这样的东西:

public CModul()
{
    sName = "";
    sKlasse = "";

    categories = new List<CModul_Category>();
}

public CModul_Category()
{
    cName = "";

    ioList = new List<CModul_I_O>();
}

它应该是这样的:

<TreeView Grid.Column="0" ItemsSource="{Binding}">
    <TreeView.Resources>
        <HierarchicalDataTemplate 
            DataType="{x:Type local:CModul}"
            ItemsSource="{Binding categories}"
            >
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding sName, Mode=OneTime}" Margin="2,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate 
            DataType="{x:Type local:CModul_Category}"
            ItemsSource="{Binding ioList">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding cName, Mode=OneTime}" Margin="2,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate 
            DataType="{x:Type local:CModul_I_O}">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding sName, Mode=OneTime}" Margin="2,0"/>
            </StackPanel>
        </HierarchicalDataTemplate>