How can I refresh a custom wpf user control from code behind?

51,127

in Window1.xaml.cs -

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!!

Share:
51,127
Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on October 19, 2020

Comments

  • Angry Dan
    Angry Dan over 3 years

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

    • Anvaka
      Anvaka over 14 years
      Why don't you just create a Refersh() method and call ShowCustomerControl.Refresh()?
  • Angry Dan
    Angry Dan over 14 years
    thanks, that was simple, here is my full code example I built up from this: tanguay.info/web/index.php?pg=codeExamples&id=319