将treeview中的selecteditem设置为数据库自定义项目wpf

时间:2016-10-22 19:02:01

标签: c# wpf xaml data-binding treeview

我正在使用TreeViewListView在C#和WPF中构建文件资源管理器,两者都绑定到名为DataItem的类。当我按下要导入的项目时,它的孩子会转到ListView。但我无法在TreeView中设置所选项目并将其消耗,因为TreeView的属性SelectedItem是只读的。如何从TreeView将我要选择的项目设置为ListView? 我在网上查了一下并尝试了不同类型的解决方案,但没有运气。

我的数据项代码(C#):

public abstract class DataItem
{
    private ObservableCollection<DataItem> FolderItems;

    public String Title {get; protected set; }

    public DataItem()
    {
        this.FolderItems = new ObservableCollection<DataItem>();

        this.Title = "";
    }

    public DataItem(DataItem Other) 
    {
        this.FolderItems = new ObservableCollection<DataItem>();

        this.Title = Other.Title;

        foreach (DataItem Folder in Other.FolderItems)
        {
            this.FolderItems.Add(Folder);
        }
    }

    public int Count()
    {
        int DataItemsCounter = 0;

        List<DataItem> FilesList = new List<DataItem>();

        foreach (DataItem File in this.Files)
        {
            FilesList.Add(File);

            DataItemsCounter++;
        }

        const int Empty = 0;

        const int FirstItem = 0;

        while (FilesList.Count != Empty)
        {
            DataItem File = FilesList[FirstItem];

            FilesList.RemoveAt(FirstItem);

            ObservableCollection<DataItem> Folder = File.Files;

            foreach (DataItem SubFile in Folder)
            {
                FilesList.Add(SubFile);

                DataItemsCounter++;
            }

        }

        return DataItemsCounter;
    }

    public virtual ObservableCollection<DataItem> Open()
    {
        return FolderItems;
    }

    public virtual ObservableCollection<DataItem> Files
    {
        get
        {
            return FolderItems;
        }
    }

    public ObservableCollection<DataItem> Folders
    {
        get
        {
            return FolderItems;
        }
    }

    public abstract String Format { get; }

    public String Icon
    {
        get
        {
            return String.Format("Icons/{0}.png", this.Format);
        }
    }
}

我的列表视图代码和TreeView(WPF):

<TreeView x:Name="FileExplorerTreeView" SelectedItemChanged="FileExplorerTreeView_SelectedItemChanged">
                        <TreeView.ItemTemplate>
                            <HierarchicalDataTemplate DataType="{x:Type local:DataItem}" ItemsSource="{Binding Folders}">
                                <DockPanel>
                                    <TextBlock Name="CaptionTextblock" Text="{Binding Title}"  DockPanel.Dock="Right"/>
                                    <Image Source="{Binding Icon, Converter={StaticResource EmptyImageToImageSourceConverter}}" RenderOptions.BitmapScalingMode= "HighQuality" Stretch="Uniform" Height="{Binding ElementName=CaptionTextblock,Path=ActualHeight}" DockPanel.Dock="Left"/>
                                </DockPanel>
                            </HierarchicalDataTemplate>
                        </TreeView.ItemTemplate>
                    </TreeView>

                    <ListView x:Name="FileExplorerListView" Grid.Column="1" Margin="4,0,0,0" ItemsSource= "{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                          SelectedItem="{Binding DataItem, RelativeSource={RelativeSource AncestorType=Window}}" SelectionChanged="FileExplorerListView_SelectionChanged">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <WrapPanel/>
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <DockPanel>
                                    <TextBlock DockPanel.Dock="Bottom" Text="{Binding Title}"/>
                                    <Image Source="{Binding Icon, Converter={StaticResource EmptyImageToImageSourceConverter}}" Width="32" Height="32" DockPanel.Dock="Top"/>
                                </DockPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>

1 个答案:

答案 0 :(得分:0)

让它工作,树视图的项目的setter不起作用,因为,项目本身没有显示在树视图上(他的父母被折叠)我没有实现inotifypropertychanged界面所以我更改项目当它被选中时,所有它的父母将被花费(因为它需要重做我项目的一半)并且我实现了inotifypropertychanged界面,现在它的工作就像一个魅力。