Windows Vista Forums

WPF UI not Refreshing

  1. #1


    Simon Guest

    WPF UI not Refreshing

    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 System SpecsSystem Spec

  2. #2


    Horst Klein Guest

    RE: WPF UI not Refreshing

    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 System SpecsSystem Spec

  3. #3


    Simon Guest

    RE: WPF UI not Refreshing

    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 System SpecsSystem Spec

WPF UI not Refreshing

Similar Threads
Thread Forum
Explorer Not Refreshing
When I delete a folder or file or create a new folder Explorer doesn't refresh. I need to close and reopen explorer to reflect any changes. Is...
Vista General
IMAP refreshing?
Every once in a while my entire "Inbox" reloads or synchs. What's the reason for this? I keep my inbox down in size but if I had a large "Inbox",...
Vista mail
SCVMM refreshing
Ok, I am sooooo new to the Hyper V world so pardon my multiple posts. I have a simple need but it's turning out to be not so simple. We are...
Virtual Server
Explorer Keeps Refreshing
I have Backgammon on my Explorer page but I cannot plan as Explorer refreshes every few minutes on its own. Annoying yes but the issue causes...
Vista performance & maintenance
Folders Not Refreshing
I have a PC running Vista Business 32-bit connected to a server running SBS 2003 R2. Whenver I save a file to a share on the server the file does...
Vista General
Re: Refreshing Web Pages in IE7
Does the Vista machine connect via wireless, via a router or directly to the cable modem? If the either of the former, does connecting directly to...
Vista security
Icons not refreshing...
I develop a Visual Basic ver 6 App (WHOS-IN In Out Board) and just finished creating a nice new aero style Icon for it in multiple sizes from 16x16...
Vista General