11

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>

4 Answers 4

3

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>
Sign up to request clarification or add additional context in comments.

Comments

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>

3 Comments

Hm, doesn't look like I can access resources in ListView if those resources are defined within ListView.Resources :/ What a strange design decision.
I ended up moving the resource I needed to App.xaml.
An example as an answer would be great.
0

You can't add resources to a Window. But you can add resources like below depending on the scope you need those resources.

App project (App.xaml)

<Application
    x:Class="WinUIDemoApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUIDemoApp">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!--  Other merged dictionaries here  -->
            </ResourceDictionary.MergedDictionaries>
            <!--  Other app resources here  -->
            <SolidColorBrush x:Key="AppProjectScopeBrush" Color="SkyBlue" />
        </ResourceDictionary>
    </Application.Resources>
</Application>

Page/Control

<Page
    x:Class="WinUIDemoApp.SomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:WinUIDemoApp"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <Page.Resources>
        <SolidColorBrush x:Key="SomePageScopeBrush" Color="SkyBlue" />
    </Page.Resources>

    <Grid RowDefinitions="Auto,*">
        <StackPanel Grid.Row="0">
            <Button Background="{StaticResource SomePageScopeBrush}" />
            <!--
                DOES NOT WORK: Can not find the ListViewScopeBrush.
                <Button Background="{StaticResource ListViewScopeBrush}" />
            -->
        </StackPanel>
        <ListView Grid.Row="1">
            <ListView.Resources>
                <SolidColorBrush x:Key="ListViewScopeBrush" Color="SkyBlue" />
            </ListView.Resources>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Button
                        Foreground="{StaticResource SomePageScopeBrush}"
                        Background="{StaticResource ListViewScopeBrush}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </Grid>

</Page>

Comments

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

1 Comment

Where are the rest from your answer?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.