How do I access Window Resources in WinUI 3
Asked Answered
V

3

9

I've just started working on a WinUi 3 (Desktop) project and have got blocked trying to add Window resources to a Window.

I would have thought the following would work. However there appears to be no Resources property.

<Window.Resources>
    
</Window.Resources>
Virescent answered 23/11, 2020 at 11:45 Comment(0)
P
2

Apparently it was a design choice, to quote Miguel, a member of Microsoft, from a Github issue:

[...] the Window object doesn't have Resource property like WPF, for instance. It was a design decision [...]

The alternative is to use the dictionary within the component's context, for instance:

<ListView>
  <ListView.Resources>
   <!-- My resources -->
  </ListView.Resources>
</ListView>
Polished answered 4/9, 2021 at 3:15 Comment(3)
Hm, doesn't look like I can access resources in ListView if those resources are defined within ListView.Resources :/ What a strange design decision.Submaxillary
I ended up moving the resource I needed to App.xaml.Submaxillary
An example as an answer would be great.Glidewell
M
2

You can declare the resource in the root control of your window.

<Window ...>
    <Border ...>
        <Border.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10"/>
            </Style>
        </Border.Resources>
        <Button ... />
    </Border>
</Window>
Maravedi answered 30/5, 2023 at 21:37 Comment(0)
C
-1

You're right - there are no Window resources. You can have globally shared resources by making App resources, or you can have <page> and <Control> level resources.

What your probably want is to define your resource dictionaries in App.xml

<Application
x:Class="BOMCheck.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
xmlns:helpers="using:BOMCheck.Helpers"
xmlns:local="using:BOMCheck"
xmlns:media="using:Microsoft.UI.Xaml.Media"
RequestedTheme="Light">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
Comintern answered 27/4, 2022 at 18:25 Comment(1)
Where are the rest from your answer?Glidewell

© 2022 - 2025 — McMap. All rights reserved.