在基于ListView的Multicolumn树视图中自动调整模板化列

时间:2009-06-18 16:33:51

标签: wpf listview treeview autosize

我正在使用我在此处找到的多列树视图控件http://www.codeproject.com/KB/WPF/wpf_treelistview_control.aspx

此控件中包含模拟树视图控件的第一列需要在展开/折叠节点时自动调整。

任何帮助?

示例XAML

<UserControl x:Class="ListViewAsTreeView.XmlTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:ListViewAsTreeView"    
xmlns:tree="clr-namespace:Aga.Controls.Tree;assembly=Aga.Controls">

<UserControl.Resources>        
    <local:RegImageConverter x:Key="RegImageConverter"/>
    <local:ComboList x:Key="MyComboSource"/>        
</UserControl.Resources>

<StackPanel>
    <tree:TreeList Name="_tree" local:DragAndDrop.DropEnabled="true"
                   MinHeight="40"
                   IsSynchronizedWithCurrentItem="True">
        <tree:TreeList.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Name">                     
                        <GridViewColumn.CellTemplate>                  
                            <DataTemplate>
                                <StackPanel 
                                    Orientation="Horizontal">
                                    <tree:RowExpander/>
                                    <Image
                                        Source="{Binding 
                                        Converter={StaticResource RegImageConverter}}"  Margin="0, 0, 5, 0"/>
                                    <TextBlock
                                        Text="{Binding Name}">                                    
                                    </TextBlock>
                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn Header="Type" Width="Auto" 
                                    DisplayMemberBinding="{Binding Kind, UpdateSourceTrigger=PropertyChanged}"/>
                    <GridViewColumn Header="Data" Width="Auto" 
                                    DisplayMemberBinding="{Binding Data, UpdateSourceTrigger=PropertyChanged}"/>
                    <GridViewColumn Header="ComboSample" Width="Auto">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ComboBox Name="MyComboBox" ItemsSource="{StaticResource MyComboSource}" 
                                          IsEditable="True" IsEnabled="True" 
                                          Text="{Binding Name}">
                                </ComboBox>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </tree:TreeList.View>
    </tree:TreeList>

    <ListBox local:DragAndDrop.DragEnabled="true">
        <ListBoxItem>Item 1</ListBoxItem>
        <ListBoxItem>Item 2</ListBoxItem>
        <ListBoxItem>Item 3</ListBoxItem>
    </ListBox>
</StackPanel>

谢谢, Jithu

2 个答案:

答案 0 :(得分:1)

     Output : 
       Parent         Col1    Col2    Col3  
       |
       |____ Child    Data1   Data2  Data3
       |
       |____ Child2   Data1    Data2  Data3

http://www.go-mono.com/mono-downloads/download.html ..下载Gtksharp用于你的操作系统并在visual studio atk-sharp.dll,gdk-sharp.dll,glib-sharp.dll,gtk-sharp.dll中添加这些dll的引用并使用使用Gtk; ...你将获得TreeView

public class TreeViewExample
      {
public static void Main()
{
    Gtk.Application.Init();
    new TreeViewExample();
    Gtk.Application.Run();
}

public TreeViewExample()
{
    Gtk.Window window = new Gtk.Window("TreeView Example");
    window.SetSizeRequest(500, 200);

    Gtk.TreeView tree = new Gtk.TreeView();
    window.Add(tree);

    Gtk.TreeViewColumn Parent = new Gtk.TreeViewColumn();
    Parent.Title = "Parent";
    Gtk.CellRendererText Parent1 = new Gtk.CellRendererText();
    Parent.PackStart(Parent1, true);

    Gtk.TreeViewColumn ChildColoumn1 = new Gtk.TreeViewColumn();
    ChildColoumn1.Title = "Column 1";           
    Gtk.CellRendererText Child1 = new Gtk.CellRendererText();
    ChildColoumn1.PackStart(Child1, true);

    Gtk.TreeViewColumn ChildColumn2 = new Gtk.TreeViewColumn();
     ChildColumn2.Title = "Column 2";
    Gtk.CellRendererText Child2 = new Gtk.CellRendererText();
    ChildColumn2.PackStart(Child2, true);

    tree.AppendColumn(Parent);
    tree.AppendColumn(ChildColoumn1);
    tree.AppendColumn(ChildColumn2);

    Parent.AddAttribute(Parent1, "text", 0);
    ChildColoumn1.AddAttribute(Child1, "text", 1);
    ChildColumn2.AddAttribute(Child2, "text", 2);

    Gtk.TreeStore Tree = new Gtk.TreeStore(typeof(string), typeof(string), typeof(string));
    Gtk.TreeIter iter = Tree.AppendValues("Parent1");
    Tree.AppendValues(iter, "Child1", "Node 1");

    iter = Tree.AppendValues("Parent2");
    Tree.AppendValues(iter, "Child1", "Node 1","Node 2");          

    tree.Model = Tree;
    window.ShowAll();
}
}

答案 1 :(得分:0)

尝试定义单独的DataGrid,并使用包含最长列值的项填充。然后DataBind Aga.Controls Treeview的列宽到DataGrid的相应列宽。这样,TreeView控件的列宽设置为最长的列项。您始终可以通过将不透明度设置为零来隐藏DataGrid。

例如:假设DataGrid名称为“TestDataGrid”,并且它有一个名为“dgNameColumn”的列

<GridViewColumn Header="Name" **Width="{Binding ElementName=dgNameColumn, Path=ActualWidth}"**>                     
                    <GridViewColumn.CellTemplate>                  
                        <DataTemplate>
                            <StackPanel 
                                Orientation="Horizontal">
                                <tree:RowExpander/>
                                <Image
                                    Source="{Binding 
                                    Converter={StaticResource RegImageConverter}}"  Margin="0, 0, 5, 0"/>
                                <TextBlock
                                    Text="{Binding Name}">                                    
                                </TextBlock>
                            </StackPanel>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>

希望这有帮助。