Comment puis-je actualiser un wpf personnalisé de contrôle de l'utilisateur à partir du code qui est derrière?

J'ai cette coutume wpf de contrôle de l'utilisateur:

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">
    <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
    }
}

Et je l'affiche à partir de ce code 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>

Et je voudrais mettre à jour la commande (dans cet exemple le courant du temps) de mon gestionnaire d'événement dans le 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()???
        }
    }
}

Comment puis-je forcer un rafraîchissement de mon contrôle personnalisé à partir de code derrière, ou le forcer à recharger une certaine manière donc, quand je clique sur le bouton, il affiche l'heure actuelle?

  • Pourquoi ne pas simplement créer un Refersh() la méthode et l'appel ShowCustomerControl.Refresh()?