簡體   English   中英

如何從后面的代碼刷新自定義WPF用戶控件?

[英]How can I refresh a custom wpf user control from 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()???
        }
    }
}

如何從后面的代碼中強制刷新自定義控件,或以某種方式強制重新加載它,所以當我單擊顯示當前時間的按鈕時?

在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();
    }

希望這可以幫助!!

或者在ShowWindow上具有LastUpdate屬性並進行設置,然后重新生成Message屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM