GithubHelp home page GithubHelp logo

Comments (7)

kmgallahan avatar kmgallahan commented on June 18, 2024 1

Your sample project has 2 problems.

  1. You are using x:DataType in a ResourceDictionary that does not have a code-behind file
  2. You have a self-referential infinite loop of ContentTemplateSelector, as RootControl uses a selector that can select it

Getting rid of the infinite loop and doing this works:

<?xml version="1.0" encoding="utf-8" ?>
<Window
  x:Class="DockToy.MainWindow"
  xmlns:controls="using:DockToy.controls"
  xmlns:local="using:DockToy"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

  <Page>
    <Page.Resources>
      <ResourceDictionary
        xmlns:local="using:DockToy"
        xmlns:models="using:DockToy.models"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">

        <DataTemplate x:Key="RootDockTemplate" x:DataType="models:IRoot">
          <local:RootControl Content="{x:Bind Content}" />
        </DataTemplate>

        <DataTemplate x:Key="DocumentDockTemplate" x:DataType="models:IDocument">
          <local:DocumentControl Content="{x:Bind Content}" />
        </DataTemplate>

        <local:DockTemplateSelector
          x:Key="DockControlSelector"
          DocumnentDockTemplate="{StaticResource DocumentDockTemplate}"
          RootDockTemplate="{StaticResource RootDockTemplate}" />
      </ResourceDictionary>
    </Page.Resources>
    <local:DockControl ContentTemplateSelector="{StaticResource DockControlSelector}">
      <controls:RootDock>
        <controls:DocumentDock>
          <TextBlock Text="I am a TextBlock" />
        </controls:DocumentDock>
      </controls:RootDock>
    </local:DockControl>
  </Page>
</Window>

Note that I used x:Bind here because as it has better performance, but Binding can be used if required.

Also note that a ContentControl, like UserControl, has special requirements on resource lookup:

https://learn.microsoft.com/en-us/windows/apps/design/style/xaml-resource-dictionary#usercontrol-usage-scope

from microsoft-ui-xaml.

bpulliam avatar bpulliam commented on June 18, 2024 1

Let us know if the suggestions don't work for you.

from microsoft-ui-xaml.

github-actions avatar github-actions commented on June 18, 2024

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

from microsoft-ui-xaml.

GochenRyan avatar GochenRyan commented on June 18, 2024

Thanks for the suggestion. While @kmgallahan recommendation is indeed effective, it doesn't align with the specific requirements I aim to address. In fact, I intend to create a WinUI Class Library, so ultimately, I won't have a Window.

Regarding the first problem, placing DataTemplates under ResourceDictionary is intended for inclusion in the MergedDictionaries section of app.xaml for sharing purposes. By setting ContentTemplateSelector on the subclass of ContentControl, for each Model object in the node tree Dock-RootDock-DocumentDock, DockControlSelector helps it find the corresponding control.

As for the second problem, to be honest, I haven't quite grasped it. Could you please provide further clarification on how I can replicate it? I've attempted to by placing breakpoints in SelectTemplateCore, but I haven't observed an infinite loop.

from microsoft-ui-xaml.

GochenRyan avatar GochenRyan commented on June 18, 2024
<local:DockControl ContentTemplateSelector="{StaticResource DockControlSelector}">
    <controls:RootDock>
        <controls:DocumentDock>
            <TextBlock Text="I am a TextBlock" />
        </controls:DocumentDock>
    </controls:RootDock>
</local:DockControl>

If I write it this way, I can only set the control corresponding to RootDock, and subsequent child nodes cannot be processed.

from microsoft-ui-xaml.

kmgallahan avatar kmgallahan commented on June 18, 2024

Here in RootControl you state you want DockControlSelector to be used to choose its content:

https://github.com/GochenRyan/WinUISample/blob/ba02c9cdbe365f69fb57f7b4d382ab724a205ae6/WinUISample/DockToy/RootControl.xaml#L10

And here DockControlSelector states it can pick RootControl to be the content:

https://github.com/GochenRyan/WinUISample/blob/ba02c9cdbe365f69fb57f7b4d382ab724a205ae6/WinUISample/DockToy/DockTemplate.xaml#L26

from microsoft-ui-xaml.

GochenRyan avatar GochenRyan commented on June 18, 2024

Maybe I should create a new control to apply the selector to all nodes in its content

from microsoft-ui-xaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.