将树视图中的selecteditem设置为与数据绑定的自定义项wpf

本文关键字:数据绑定 自定义 wpf 设置 视图 selecteditem | 更新日期: 2023-09-27 18:14:16

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

我的数据项代码(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>

将树视图中的selecteditem设置为与数据绑定的自定义项wpf

得到了它的工作,treeview项目的setter没有工作,因为,项目本身没有显示在treeview(他的父母被崩溃了),我没有实现inotifypropertychanged接口,所以我改变了项目,当它被选中时,它的所有父母都将被扩展(需要重做我的项目的一半,因为它),我实现了inotifypropertychanged接口,现在它的工作就像一个魅力。