Click here to Skip to main content
14,869,990 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here my code
C#
public class NodeDetails
{
    public string Name { get; set; }
    public string Size { get; set; }
    public ushort DateModified { get; set; }
    public string FType { get; set; }
    public string Path { get; set; }
    public ImageSource ListIcon { get; set; }
}

public class ViewModel : ViewModelBase
{
    private NodeDetails _student;
    private ObservableCollection<nodedetails> _students;
    private ICommand _SubmitCommand;
    public NodeDetails Student
    {
        get
        {
            
            return _student;
        }
        set
        {
            _student = value;
            NotifyPropertyChanged("Student");
        }
    }
    public ObservableCollection<nodedetails> Students
    {
        get
        {
        //  _students= new ObservableCollection<nodedetails>(Global.GnodeDetails);
            return _students;
        }
        set
        {
            _students = value;
            NotifyPropertyChanged("Students");
        }
    }

    public ICommand SubmitCommand
    {
        get
        {
            if (_SubmitCommand == null)
            {
                _SubmitCommand = new RelayCommand(param => this.Submit(),
                    null);
            }
            return _SubmitCommand;
        }
    }
    public ViewModel()
    {
        Student = new NodeDetails();
        Students = new ObservableCollection<nodedetails>();
    
        //Students = new ObservableCollection<nodedetails>(Global.GnodeDetails);
        Students.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Students_CollectionChanged);
        
    }

public  void Students_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        NotifyPropertyChanged("Students");
    }

    public void Submit()
    {
        Students = new ObservableCollection<nodedetails>(Global.GnodeDetails);

        

        //Students.Add(Student);
        //Student = new NodeDetails();
    }
}


public class ViewModelBase : INotifyPropertyChanged
{


    public event PropertyChangedEventHandler PropertyChanged;
//public event EventHandler SomeEventHappened;

    protected void NotifyPropertyChanged(string propertyName)

    {

        

        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class RelayCommand : ICommand
{
    public RelayCommand(Action<object> execute) : this(execute, null)
    {
    }
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter)
    {
        _execute(parameter);
    }
    private readonly Action<object> _execute;
    private readonly Predicate<object> _canExecute;
}

////Xaml code
XAML
<local:treeviewcontrol grid.column="0" background="#F9F9F9" horizontalalignment="Left" verticalalignment="Top" height="540" width="230" margin="0,0,0,0">
    <listview grid.column="1" itemssource="{Binding Students}">
        <listview.view>
            <gridview>
                <gridviewcolumn header="Name" celltemplate="{StaticResource myCellTemplateMonth}" width="200">
                    
                
                <gridviewcolumn header="Size" width="60" displaymemberbinding="{Binding Size}">
                <gridviewcolumn header="DateModified" width="150" displaymemberbinding="{Binding DateModified}">
                <gridviewcolumn header="Type" width="70" displaymemberbinding="{Binding FType}">
                <gridviewcolumn header="Path" width="150" displaymemberbinding="{Binding Path}">
            

<pre>
<pre> <Button Height="40" Width="105" Margin="0,0,20,0" Grid.Column="1"  Command="{Binding SubmitCommand}" HorizontalAlignment="Right" Background="#8E8E8E">

    <StackPanel>
        <TextBlock Text="Recover" FontSize="18" Foreground="#F9F9F9"/>
    </StackPanel>
</Button>

My problem is when I use the Recover button the listview is update in UI. I have given above one treecontrol(custom control) in that view model if I get any value I need to update the UI cant able to use the list view from other classes.

What I have tried:

class tree
{
    public void accesss()
    {
        Viewmodel tem=new Viewmodel();
        tem.submit();
    }
}

I need to access the list from other class
Posted
Updated 16-Jan-21 5:06am
v5
Comments
Gerry Schmitz 15-Jan-21 8:08am
   
I don't see you setting the DataContext anywhere or otherwise hooking up "Students".

For the most part, your "question" is not a question and makes no sense.
Fazil13 15-Jan-21 8:12am
   
i cant able to post a full code,i placed <local:viewmodel x:key="ViewModel"> in the mainwindowxaml.FYI i am new to wpf
Gerry Schmitz 16-Jan-21 12:01pm
   
If you're new to WPF, you shouldn't be confusing your brain with the confusion known as MVVM. MVVM is an abstraction that removes you from understanding the underlying platform.
Fazil13 16-Jan-21 11:19am
   
Gerry Schmitz ...take a look here i tried my best to explain my problem

https://stackoverflow.com/questions/65740888/how-to-bind-the-listview-data-from-another-class-in-wpf
Gerry Schmitz 16-Jan-21 12:18pm
   
I did. If you toss the MVVM, you could eliminate at least 50% of the code. Learn the basics first.

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-create-and-bind-to-an-observablecollection?view=netframeworkdesktop-4.8

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/data/how-to-make-data-available-for-binding-in-xaml?view=netframeworkdesktop-4.8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900