Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF Data-Binding
Hi, I have got a Task class:
    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:
<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:
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 16-Oct-12 6:10am
Edited 16-Oct-12 6:46am
v2
Comments
Wes Aday at 16-Oct-12 11:14am
   
And so why are you reposting this?
cs101000 at 16-Oct-12 11:24am
   
I didnt get my answer and Im going to delete the previous one.
Wes Aday at 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 at 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.
Wes Aday at 16-Oct-12 11:36am
   
Did you try DataContext = new Task()? What is "MainTask"? What error messages to you see int he debug window?
cs101000 at 16-Oct-12 11:47am
   
Take a look at the improved part please. I dont get any error messages.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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;
    }
  Permalink  
v2
Comments
cs101000 at 16-Oct-12 16:04pm
   
Thanks a lot for the help
Clifford Nelson at 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 at 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)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 19 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100