views:

738

answers:

2

I have this custom wpf user control:

ShowCustomer.xaml:

<UserControl x:Class="TestControlUpdate2343.Controls.ShowCustomer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
    <Grid>
        <TextBlock Text="{Binding Message}"/>
    </Grid>
</UserControl>

ShowCustomer.xaml.cs:

using System.Windows.Controls;
using System;
using System.ComponentModel;

namespace TestControlUpdate2343.Controls
{
    public partial class ShowCustomer : UserControl, INotifyPropertyChanged
    {
        #region ViewModelProperty: Message
        private string _message;
        public string Message
        {
            get
            {
                return _message;
            }

            set
            {
                _message = value;
                OnPropertyChanged("Message");
            }
        }
        #endregion

        public ShowCustomer()
        {
            InitializeComponent();
            DataContext = this;

            Message = "showing test customer at: " + DateTime.Now.ToString();
        }

        #region INotifiedProperty Block
        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

And I display it from this XAML:

Window1.xaml:

<Window x:Class="TestControlUpdate2343.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:TestControlUpdate2343.Controls"
    Title="Window1" Height="300" Width="300">
    <StackPanel HorizontalAlignment="Left" Margin="10">
        <controls:ShowCustomer x:Name="ShowCustomerControl" Margin="0 0 0 10"/>
        <Button Content="Refresh Control" 
                Click="Button_RefreshControls_Click"
                Margin="0 0 0 10"/>
    </StackPanel>
</Window>

And I would like to update the control (i.e. in this example show the current time) from my event handler in code behind:

using System.Windows;

namespace TestControlUpdate2343
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void Button_RefreshControls_Click(object sender, RoutedEventArgs e)
        {
            //ShowCustomerControl.Refresh()???
        }
    }
}

How can I force a refresh of my custom control from code behind, or force it to reload somehow so when I click the button it shows the current time?

A: 

Or have a LastUpdate property on ShowWindow and set that, which then regenerates the Message property.

+1  A: 

in Window1.xaml

private void Button_RefreshControls_Click(object sender, RoutedEventArgs e)
{
    ShowCustomerControl.Refresh();
}

in ShowCustomer.xaml.cs

    public ShowCustomer()
    {
        InitializeComponent();
        DataContext = this;

        Refresh();
    }

    public void Refresh()
    {
        Message = "showing test customer at: " + DateTime.Now.ToString();
    }

Hope this helps!!

viky
Edward Tanguay