Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have got a Task class:
C#
public class Task : INotifyPropertyChanged
{
    private string taskName;
    private string description;
    public string Description
    {
        get { return description; }
        set
        {
            description = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Description"));
        }
    }

    public string TaskName
    {
        get { return taskName; }
        set
        {
            taskName = value;
            OnPropertyChanged(new PropertyChangedEventArgs("TaskName"));
        }

    }
    private ObservableCollection<Task> subTasks;
    public ObservableCollection<Task> SubTasks
    {
        get { return subTasks; }
        set
        {
            subTasks = value;
            OnPropertyChanged(new PropertyChangedEventArgs("SubTasks"));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    //ctors
    public Task(string strTaskName)
    {
        TaskName = strTaskName;
        //SubTasks = new ObservableCollection<Task>();
    }
    public Task(string strTaskName, string strDescription)
    {
        TaskName = strTaskName;
        Description = strDescription;
        //SubTasks = new ObservableCollection<Task>();
    }

    public Task(string strTaskName, ObservableCollection<Task> subs)
    {
        TaskName = strTaskName;
        SubTasks = subs;
    }
}


that needs its Description property bound to a textbox control with this XAML:
HTML
<Window x:Class="Task_it.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TreeView Name="treeTasks" Grid.Column="1" Margin="5">
            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Path=SubTasks}">
                    
                    <TextBlock Text="{Binding Path=TaskName}" />
                    
                </HierarchicalDataTemplate>               
            </TreeView.ItemTemplate>
        </TreeView>

        <TextBox  Name="txtDescription" Grid.Column="0" VerticalAlignment="Top" Margin="5" Text="{Binding Path=Description}">     
        </TextBox>
            
       
    </Grid>
</Window>

I am new to WPF and I dont know where the problem occurs that the text box is not showing the description. And this is the main window:
C#
InitializeComponent();
Task job11 = new Task("JOB 1.1","This is job 1.1");
            Task job12 = new Task("JOB 1.2", "This is job 1.2");
            Task job13 = new Task("JOB 1.3", "This is job 1.3");
            Task job14 = new Task("JOB 1.4","This is job 1.4");
            Task MainTask = new Task("Main Job", new ObservableCollection<Task>(new List<Task> { job11, job12, job13, job14 }));
            MainTask.Description = "This is my dear MAINTASK";
txtDescription.DataContext = MainTask;
 treeTasks.Items.Add(MainTask);
            treeTasks.FlowDirection = System.Windows.FlowDirection.RightToLeft;
Posted
Updated 16-Oct-12 5:46am
v2
Comments
[no name] 16-Oct-12 11:14am    
And so why are you reposting this?
cs101000 16-Oct-12 11:24am    
I didnt get my answer and Im going to delete the previous one.
[no name] 16-Oct-12 11:26am    
So? Why? You did your answer, you did not set the datacontext. You know what the problem is, why don't you go off and fix it?
cs101000 16-Oct-12 11:33am    
What I inferred from that is I have to set the datacontext in main window ctor like this:
txtDescription.DataContext = MainTask;

which is not working.
[no name] 16-Oct-12 11:36am    
Did you try DataContext = new Task()? What is "MainTask"? What error messages to you see int he debug window?

1 solution

Unfortunately there is no SelectedItem read/write dependency property for WPF. You can create a behaviour to get around this (http://stackoverflow.com/questions/1000040/selecteditem-in-a-wpf-treeview[^]), or use code behind for the SelectedItemChanged.

Ideally you should have a view model with an ItemsSource and SelectedItem property, and bind to each. The quick fix is to used the SelectedItemChanged event, and set the Text property of the TextBox to the Description of the new selected item:

C#
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitializeComponent();
        Task job11 = new Task("JOB 1.1", "This is job 1.1");
        Task job12 = new Task("JOB 1.2", "This is job 1.2");
        Task job13 = new Task("JOB 1.3", "This is job 1.3");
        Task job14 = new Task("JOB 1.4", "This is job 1.4");
        Task MainTask = new Task("Main Job", new ObservableCollection<Task>(new List<Task> { job11, job12, job13, job14 }));
        MainTask.Description = "This is my dear MAINTASK";
        txtDescription.DataContext = MainTask;
        treeTasks.Items.Add(MainTask);
        treeTasks.FlowDirection = System.Windows.FlowDirection.RightToLeft;

        treeTasks.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(treeTasks_SelectedItemChanged);
    }

    void treeTasks_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
        var task = (Task) e.NewValue;
        txtDescription.Text = task.Description;
    }
 
Share this answer
 
v2
Comments
cs101000 16-Oct-12 16:04pm    
Thanks a lot for the help
Clifford Nelson 16-Oct-12 16:08pm    
I do not know if you went the behaviour way, or the straight forward way, but good luck. Would be nice is you rated the answer and marked it as answered.

Clifford
cs101000 18-Oct-12 3:19am    
I went the straightforward way and that was successful after removing the line Description.DataContext = MainTask;
which caused a logical error and also changing the treeTasks_SelectedItemChanged to make the binding 2-way(using e.OldValue). Now there is a tedious new problem with removing a node. Would you take a look at this please? (solution below)
P.S: Solution accepted and rated

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