Click here to Skip to main content
15,885,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The scenario is to bind the WordsNum property of article object to a textblock so that it is updated when the property changes. I have spent hours on debugging this but it seems to be an ugly error!
HTML
<Window x:Class="Article_Word_Counter.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" FontSize="14">
    <Grid>
        <StackPanel>
            <Label>Choose your article:</Label>
            <TextBox Name="txtFilePath" Height="30"></TextBox>
            <Button Name="btnBrowse" Height="30" Width="150" Click="btnBrowse_Click">Browse</Button>
            <GroupBox Header="Article Info"> 
                <StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <Label>Numder of Words: </Label>
                        <TextBlock Text="{Binding Path=WordsNum, UpdateSourceTrigger=PropertyChanged}"></TextBlock> 
                    </StackPanel>
                    
                </StackPanel>
            </GroupBox>
        </StackPanel>
    </Grid>
</Window>


C#
namespace Article_Word_Counter
{
    public partial class MainWindow : Window
    {
        public Article CurrentArticle { get; set; }
        public MainWindow()
        {
            InitializeComponent();
            base.DataContext = CurrentArticle;  
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            //logic for openfiledialog
            //strFilePath is obtained from above logic
            CurrentArticle = new Article(strFilePath);
        }
    }
    public class Article: INotifyPropertyChanged
    {
        private int wordsNum;
        public int WordsNum 
        {
            get
            {
                return wordsNum;
            }
            set
            {
                wordsNum = value;
                this.OnPropertyChanged("WordsNum");
            }
        }
        
        #region ctors
        //public 
        public Article(string strFilePath)
        {
            //logic for calculatin intCount 
            WordsNum = intCount;
        }

        #endregion

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        #endregion
    }
}
Posted
Updated 14-Apr-13 22:45pm
v2

1 solution

In btnBrowse_Click you set the value of CurrentArticle but the DataContext still keeps the old value...


You can set the DataContext too:


C#
private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
    //logic for openfiledialog
    //strFilePath is obtained from above logic
    CurrentArticle = new Article(strFilePath);
    DataContext = CurrentArticle;
}
 
Share this answer
 
v2
Comments
cs101000 15-Apr-13 5:22am    
Thank you Shmuel! Why does it keep the old value, then? Can I do it in another way so that I dont have to set datacontext every time CurrentArticle is updated?
Shmuel Zang 15-Apr-13 6:56am    
The DataContext keeps the old value because there is nothing that tells it to change its value when CurrentArticle is changed. Another way to do that can be using binding (bind DataContext to CurrentArticle). But, if you use code-behind to set the CurrentArticle, the simplest way is to set the DataContext in the line after.

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