简体   繁体   English

XAML中的绑定混乱

[英]Binding confusion in xaml

The source code can be found here : https://www.codeproject.com/Articles/24973/TreeListView 可以在这里找到源代码: https : //www.codeproject.com/Articles/24973/TreeListView

The way the original author has set it up, is the data is filled in the xaml itself. 原始作者进行设置的方式是将数据填充到xaml本身中。 I need to create the TreeViewList inside of my ViewModel, but I can't figure out how to bind my own TreeViewList within the xaml to display it properly. 我需要在ViewModel内部创建TreeViewList,但是我不知道如何在xaml中绑定我自己的TreeViewList才能正确显示它。

Here's an example of me creating a tree in the code behind and calling the window. 这是我在后面的代码中创建树并调用窗口的示例。

    public TreeListView TreeList { get; set; } = new TreeListView();

    private void generateTree()
    {
        TreeList.Columns.Add(new GridViewColumn() { Header = "Col1" });
        TreeList.Columns.Add(new GridViewColumn() { Header = "Col2" });
        TreeList.Columns.Add(new GridViewColumn() { Header = "Col3" });
    }

    public ICommand AssemblyTreeCommand => new RelayCommand(AssemblyTree, p => CanAssemblyTree);
    public bool CanAssemblyTree { get; set; } = true;
    private void AssemblyTree(object parameter)
    {
        generateTree();
        AssemblyTreeDialogWindow dialog = new AssemblyTreeDialogWindow()
        {
            DataContext = this,
            Topmost = true
        };
        dialog.ShowDialog();

    }

AssemblyTreeDialog Window class looks like this: AssemblyTreeDialog Window类如下所示:

   <local:TreeListView AllowsColumnReorder="True" ItemsSource="{Binding TreeList}">
        <!--Create an item template to specify the ItemsSource-->
        <local:TreeListView.ItemTemplate>
            <HierarchicalDataTemplate ItemsSource="{Binding Children}" />
        </local:TreeListView.ItemTemplate>
        <local:TreeListView.Columns>
            <!--Create the first column containing the expand button and the type name.-->
            <GridViewColumn Header="Name" Width="200">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--The Expander Button (can be used in any column (typically the first one))-->
                            <local:TreeListViewExpander/>
                            <!--Display the name of the DataElement-->
                            <TextBlock Text="{Binding}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!--Create a second column containing the number of children.-->
            <GridViewColumn Header="Children" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <!--Display the size of the DataElement-->
                        <TextBlock Text="{Binding Children.Count}" HorizontalAlignment="Right"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <!--Create a third column containing the brush of the material.-->
            <GridViewColumn Header="Brush" Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <!--Border showing the actual color-->
                            <Border Background="{Binding Brush}" CornerRadius="2"
                                    Width="16" Height="16"
                                    BorderThickness="1" BorderBrush="DarkGray"/>
                            <!--Display the brush-->
                            <TextBlock Text="{Binding Brush}"/>
                        </StackPanel>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </local:TreeListView.Columns>
        <!--Create some sample data-->
        <MaterialGroup>
            <MaterialGroup>
                <DiffuseMaterial Brush="Blue"/>
                <DiffuseMaterial Brush="Red"/>
                <SpecularMaterial Brush="Orange"/>
            </MaterialGroup>
            <EmissiveMaterial Brush="AliceBlue"/>
        </MaterialGroup>
    </local:TreeListView>

Interestingly if I bind the line <GridViewColumn Header="Name" Width="200"> so that it reads <GridViewColumn Header="{Binding TreeList}" Width="200"> it gives me this: 有趣的是,如果我将行<GridViewColumn Header="Name" Width="200">绑定在一起,以使其读取<GridViewColumn Header="{Binding TreeList}" Width="200">它将为我提供:

测试用例1

I'll explain my end goal as best as possible. 我会尽力解释我的最终目标。

The System is a giant list of parts. 该系统是一个庞大的零件清单。 A main table displays all of the parts while a subtable displays all of the parts which make up that part. 主表显示所有零件,而子表显示组成该零件的所有零件。 All parts (including those which are used to create other parts) exist within the MainTable. 所有的零件(包括用于创建其他零件的零件)都存在于MainTable中。 So a Parent part might have a set of children parts, which each individually have children parts which they are made up of. 因此,父部件可能具有一组子部件,每个子部件分别由它们组成的子部件。 This is the relationship i'm trying to model using this tool. 这就是我正在尝试使用此工具建模的关系。

The code that I've written maps the parts list out into a list of class objects which contain the data. 我编写的代码将零件列表映射到包含数据的类对象列表中。 I'll post it below. 我将其张贴在下面。 It's working to map to a TreeView right now. 现在正在映射到TreeView。

结果

A datastructure I've based it off is here : Treeview with multiple columns 我基于它的数据结构在这里: 具有多列的Treeview

private void generateTree(string PN)
{
    Proces selectedRow = new Proces() { procesId = (int)Vwr.Table.SelectedRow.Row["PID"], procesName = (string)Vwr.Table.SelectedRow.Row["PN"], subProcesses = generateSubtable(PN) };
    processes.Add(selectedRow);
}


public List<Proces> generateSubtable(string PN)
{
    List<Proces> subTable = new List<Proces>();

    foreach (DataRow mplrow in Vwr.Table.Tbl.Rows) if (mplrow["PN"].ToString() == PN)
            MainVM.Modules.AllModules[0].SubVwr.Tables[0].LoadTableQuery.Prms[0].Val = mplrow[0];
    MainVM.Modules.AllModules[0].SubVwr.Tables[0].Tbl = Db.GetTable(MainVM.Modules.AllModules[0].SubVwr.Tables[0].LoadTableQuery);

    foreach (DataRow sub in MainVM.Modules.AllModules[0].SubVwr.Tables[0].Tbl.Rows)
    {
        Proces subItem = new Proces() { procesId = (int)sub["ItemNo"], procesName = sub["PN"].ToString(), subProcesses = generateSubtable(sub["PN"].ToString()) };
        subTable.Add(subItem);
    }

    return subTable;
}

Found the answer! 找到了答案! After some pretty extensive searching, and trying many different solutions. 经过一些相当广泛的搜索,并尝试了许多不同的解决方案。 Thought i'd post incase someone else might also be trying to do the same. 以为我会发布,以防其他人也可能试图这样做。

credit: http://dlaa.me/blog/post/9898803 信用: http : //dlaa.me/blog/post/9898803

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM