[英]How do I know when the content of ContentControl is loaded when changing
I have a ContentControl whose content is determined by a DataTemplateSelector based on property Workspace. 我有一个ContentControl,其内容由DataTemplateSelector基于属性Workspace确定。 But when the data template is changed, I must do some calculations based on the initial size of ContentControl and the whole Window, so I want to know when it is Loaded.
但是当数据模板被更改时,我必须根据ContentControl的初始大小和整个Window进行一些计算,所以我想知道它何时被加载。
<ContentControl Content="{Binding Path=Workspace}" ContentTemplateSelector="{StaticResource workspaceTemplateSelector}" />
ResourceDictionary: ResourceDictionary中:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vw="clr-namespace:Capgemini.Sag.KeyEm.View">
<DataTemplate x:Key="keyboardTemplate" >
<vw:Keyboard/>
</DataTemplate>
<DataTemplate x:Key="welcomeTemplate">
<vw:Welcome/>
</DataTemplate>
<vw:WorkspaceTemplateSelector
KeyboardTemplate="{StaticResource keyboardTemplate}"
WelcomeTemplate="{StaticResource welcomeTemplate}"
x:Key="workspaceTemplateSelector"/>
</ResourceDictionary>
DataTemplateSelector: DataTemplateSelector:
using Capgemini.Sag.KeyEm.ViewModel.Interfaces;
namespace Capgemini.Sag.KeyEm.View
{
using System.Windows;
using System.Windows.Controls;
class WorkspaceTemplateSelector : DataTemplateSelector
{
public DataTemplate WelcomeTemplate { get; set; }
public DataTemplate KeyboardTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
if (item is IWelcomeViewModel)
return WelcomeTemplate;
if (item is IKeyboardViewModel)
return KeyboardTemplate;
return null;
}
}
}
One thing you can do is wrap your datatemplate content inside a container and listen to the loaded event 您可以做的一件事是将datatemplate内容包装在容器中并监听加载的事件
<DataTemplate x:Key="keyboardTemplate">
<Grid Loaded="Grid_Loaded">
<vw:Welcome/>
</Grid>
</DataTemplate>
the loaded event will be raised when the templates are switched.Hope this will help. 切换模板时会引发加载的事件。希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.