更改时如何知道ContentControl的内容何时加载

发布于 2024-10-07 00:13:37 字数 1738 浏览 0 评论 0原文

我有一个 ContentControl,其内容由基于属性 Workspace 的 DataTemplateSelector 确定。但是当数据模板改变时,我必须根据ContentControl的初始大小和整个Window进行一些计算,所以我想知道它什么时候加载的。

<ContentControl Content="{Binding Path=Workspace}" ContentTemplateSelector="{StaticResource workspaceTemplateSelector}" />

资源字典:

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

数据模板选择器:

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

I have a ContentControl whose content is determined by a DataTemplateSelector based on property 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 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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假面具 2024-10-14 00:13:37

您可以做的一件事是将数据模板内容包装在容器中并监听加载事件,

<DataTemplate x:Key="keyboardTemplate">
        <Grid Loaded="Grid_Loaded">
            <vw:Welcome/>
        </Grid>
    </DataTemplate>

当切换模板时将引发加载事件。希望这会有所帮助。

One thing you can do is wrap your datatemplate content inside a container and listen to the loaded event

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文