如何从代码后面刷新自定义wpf用户控件?

时间:2009-12-14 14:38:20

标签: c# wpf xaml user-controls code-behind

我有这个自定义的wpf用户控件:

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

我从这个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>

我想在代码后面的事件处理程序中更新控件(即在此示例中显示当前时间):

using System.Windows;

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

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

如何强制从后面的代码刷新我的自定义控件,或强制它以某种方式重新加载,所以当我点击按钮时它会显示当前时间?

2 个答案:

答案 0 :(得分:5)

Window1.xaml.cs中的

-

private void Button_RefreshControls_Click(object sender, RoutedEventArgs e)
{
    ShowCustomerControl.Refresh();
}
ShowCustomer.xaml.cs中的

-

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

        Refresh();
    }

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

希望这会有所帮助!!

答案 1 :(得分:0)

或者在ShowWindow上有一个LastUpdate属性并设置它,然后重新生成Message属性。