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.   


  3. #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

  4. #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
IMAP refreshing? Vista mail
SCVMM refreshing Virtual Server
Folders Not Refreshing Vista General
Re: Refreshing Web Pages in IE7 Vista security
Icons not refreshing... Vista General