• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

WPF UI not Refreshing

S

Simon

#1
Environment:
Visual Studio 2005 with WPF Ext (C#)
Windows XP

I created a usercontrol that binds to a global object, but when the value of
the global object change the fields on the UI is not refreshing. Can someone
please let me know what the problem is? Below is an example solution

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication3
{
public partial class App : System.Windows.Application
{

[STAThreadAttribute()]

public static void Main()
{
WindowsApplication3.App app = new WindowsApplication3.App();
Customer cc = new Customer();
cc.FirstName = "John";
cc.LastName = "Doe";
GlobalObj.Instance.CurrentCustomer = cc;
app.Run(new Window1());
}
}

}


<Window x:Class="WindowsApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication3" Height="300" Width="300"
<Grid>
<StackPanel>
<Frame Name="MainPageFrame" Grid.Row="1" Source="Page1.xaml"
NavigationUIVisibility="Hidden"/>

<Button Name="btChangeCustomer" Margin="0,0,6,0"
Click="OnClickChangeCustomer">Change Customer</Button>
</StackPanel>
</Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


namespace WindowsApplication3
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>

public partial class Window1 : System.Windows.Window
{

public Window1()
{
InitializeComponent();
}
void OnClickChangeCustomer(object sender, RoutedEventArgs e)
{
Customer cc1 = new Customer();
cc1.FirstName = "Jane";
cc1.LastName = "Smith";
GlobalObj.Instance.CurrentCustomer = cc1;
}
}
}


<Page x:Class="WindowsApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UI="clr-namespace:WindowsApplication3"
Title="Page1" <Grid>
<UI:UserControl1 />
</Grid>
</Page>



<UserControl x:Class="WindowsApplication3.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" <Grid x:Name="GridContainer" Margin="10" >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="6"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<Label Grid.Row="0" Grid.Column="0">FirstName</Label>
<TextBox x:Name="CFirstName" Grid.Row="0" Grid.Column="2" Width="150"
Text="{Binding FirstName,Mode=OneWay}" />
<Label Grid.Row="2" Grid.Column="0">LastName</Label>
<TextBox Grid.Row="2" Grid.Column="2" x:Name="CLastName" Width="150"
Text="{Binding LastName,Mode=OneWay}" />
</Grid>
</UserControl>



using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WindowsApplication3
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>

public partial class UserControl1 : System.Windows.Controls.UserControl
{
private delegate void MyDelegate();
public UserControl1()
{
InitializeComponent();
GridContainer.DataContext = GlobalObj.Instance.CurrentCustomer;


}

}
}



using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace WindowsApplication3
{

public class GlobalObj : INotifyPropertyChanged
{
static GlobalObj instance = new GlobalObj();
static GlobalObj()
{
}

GlobalObj()
{
}

public static GlobalObj Instance
{
get
{
return instance;
}
}
private Customer _CurrentCustomer;
public Customer CurrentCustomer
{
get { return _CurrentCustomer; }
set {
_CurrentCustomer = value;
OnPropertyChanged("CurrentCustomer");
}

}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
PropertyChanged(this, new
PropertyChangedEventArgs(propertyName));
}
}
}





using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsApplication3
{
public class Customer
{
private string _FirstName;
public string FirstName
{
get { return _FirstName; }
set { _FirstName = value; }
}

private string _LastName;
public string LastName
{
get { return _LastName; }
set { _LastName = value; }
}
}
}
 

My Computer

H

Horst Klein

#2
Hi Simon

I take a swift view into your code.
Looks like you have to implement the INotifyPropertyChanged Interface in to
your Customers class.

Best regards
Horst
 

My Computer

S

Simon

#3
The INotifyPropertyChanged was implemented but the problem was with the
TextBox control so I ending up using the ListBox control instead and it work
fined

"Horst Klein" wrote:

> Hi Simon
>
> I take a swift view into your code.
> Looks like you have to implement the INotifyPropertyChanged Interface in to
> your Customers class.
>
> Best regards
> Horst
 

My Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)