我有一個ContentControl,其內容由DataTemplateSelector根據屬性Workspace確定。但是當數據模板發生變化時,我必須根據ContentControl的初始大小和整個窗口進行一些計算,所以我想知道它何時加載。我如何知道ContentControl的內容在更改時何時加載
<ContentControl Content="{Binding Path=Workspace}" ContentTemplateSelector="{StaticResource workspaceTemplateSelector}" />
的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:
你可以做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;
}
}
}
處理此問題的正確方法是回答自己的問題,然後幾天後選擇該問題作爲正確答案。 – Will 2011-01-25 14:03:06