Edw*_*uay -1 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>
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
我从这个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>
Run Code Online (Sandbox Code Playgroud)
我想在代码后面的事件处理程序中更新控件(即在此示例中显示当前时间):
using System.Windows;
namespace TestControlUpdate2343
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Button_RefreshControls_Click(object sender, RoutedEventArgs e)
{
//ShowCustomerControl.Refresh()???
}
}
}
Run Code Online (Sandbox Code Playgroud)
如何强制从后面的代码刷新我的自定义控件,或强制它以某种方式重新加载所以当我单击按钮它显示当前时间?
private void Button_RefreshControls_Click(object sender, RoutedEventArgs e)
{
ShowCustomerControl.Refresh();
}
Run Code Online (Sandbox Code Playgroud)
public ShowCustomer()
{
InitializeComponent();
DataContext = this;
Refresh();
}
public void Refresh()
{
Message = "showing test customer at: " + DateTime.Now.ToString();
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!!
| 归档时间: |
|
| 查看次数: |
38660 次 |
| 最近记录: |