Upadate TreeView From Class .cs

本文关键字:cs Class From TreeView Upadate | 更新日期: 2023-09-27 18:14:20

大家早上好;

我有一个TreeView在我的窗口WPF,我使用数据绑定覆盖我的TreeView。

现在我有另一个类MyDesign.cs,我想从这个类更新我的TreeView的项目。

我的代码:

MainWindow.xaml:

<Window x:Class="TreeViewAndDataBanding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:self="clr-namespace:TreeViewAndDataBanding"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ContextMenu x:Key="MyDesignContextMenu">
            <MenuItem Header="Paste" Command="{x:Static ApplicationCommands.Paste}"/>
            <MenuItem Header="Search" Command="{x:Static self:MyDesign.Search}"/>
        </ContextMenu>

        <self:test x:Key="test"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="24*"/>
            <ColumnDefinition Width="23*"/>
        </Grid.ColumnDefinitions>
        <TreeView x:Name="MyToolBox"  ItemsSource="{StaticResource test}" Grid.Column="1" >
            <TreeView.ItemTemplate>
                <DataTemplate>
                        <TreeViewItem Header="All Cars">
                        <TreeViewItem Header="{Binding Path= Voiture}">
                            <TreeViewItem Header="{Binding Path=Vitesse}"></TreeViewItem>
                        </TreeViewItem>
                        </TreeViewItem>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
        <s:MyDesign Focusable="true" x:Name="MyDesigner"
                            Background="{StaticResource WindowBackgroundBrush}"
                            Margin="10" FocusVisualStyle="{x:Null}"
                            ContextMenu="{StaticResource MyDesignContextMenu}" Grid.Coulum="0"/>

    </Grid>
</Window>

MenuItem.cs

  namespace TreeViewAndDataBanding
    {
        public class MenuItem
        {
            private string _Voiture;
            private string _Vitesse;
            public MenuItem( string Voiture,string Vitesse)
            {
                this._Voiture = Voiture;
                this._Vitesse = Vitesse;
           }
            public string Voiture
            {
                get { return _Voiture; } 
            }
            public string Vitesse
            {
                get { return _Vitesse; }
            }
        }
    }

test.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace TreeViewAndDataBanding
{
    public class test : ObservableCollection<MenuItem> 
    {
       public test()
       {
           Add(new MenuItem("Rapide", "Ferrari F430"));
       }

    }
}

在这里我的类MyDesign.Command.Cs,我想能够更新我的treeView在这个类(在方法搜索)

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace TreeViewAndDataBanding
{
    public class MyDesign
    {
        public static RoutedCommand Search = new RoutedCommand();
        public MyDesign()
        {
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, Paste_Executed));
            this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, Search_Executed));
        }

        private void Search_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            // d'ici je veux modifier mon TreeView
            /******************************************************************************************/
            /****** ici je veux modifier  et mettre a jour mon Treeview dans l'interface *************/
            /****************************************************************************************/

        }

        private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
        {
        }


    }
}

你能帮帮我吗?? ?有什么想法?

Upadate TreeView From Class .cs

这样做MVVM风格更干净,更容易维护,并使WPF更有意义。举个例子(我已经重命名了一些东西,因为我的法语很好。不存在的)

创建ViewModel:

public class ViewModel {
    public ObservableCollection<CarType> CarTypes { get; private set; }
    public ViewModel() {
        CarsTypes = new ObservableCollection<CarType>();
        var sportsCars = new CarType("Sports cars");
        sportscars.Cars.Add(new Car() { Make = "Ferrari", Model = "F430" });
        CarTypes.Add(sportsCars);
    }
}

And your View:

<Window ...
    <Window.DataContext>
         <local:ViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Cars}" DataType="{x:Type local:CarType}">
            <TextBlock Text="{Binding Name}"/>
        </HierarchicalDataTemplate>
        <DataTemplate DataType="{x:Type local:Car}">
            <StackPanel>
                <TextBlock Text="{Binding Make}"/>
                <TextBlock> - </TextBlock>
                <TextBlock Text="{Binding Model}"/>
        </DataTemplate>
    </Window.Resources>
    <TreeView ItemsSource="{Binding CarTypes}"/>
</Window>

请注意,我直接在SO中输入了上面的内容,所以我没有编译它。我可能有一些错误。但是正如您所看到的,使用MVVM,这是非常少的代码。

现在您可以向ViewModel上的Collection添加新的实例,然后您的UI就会更新。命令可以使用RelayCommands在ViewModel上实现。