0

In my project (ABC), I have MainWindow.xaml where I have defined a toolbar for it and this toolbar is defined in project under Resource/Views/ApplicationToolbar.xmal as following:

I have referenced it in my MainWindow.xaml as xmlns:local="clr-namespace:ABC" and as soon as I run it I get an error indicating View123 not found.

More information: (EDIT)

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:ABC">

    <StackPanel x:Key="View1">
        <Button Margin="3" Content="Test1"></Button>
        <Button Margin="3" Content="Test2"></Button>
    </StackPanel>

</ResourceDictionary>

Now in MainWindow.xmal have:

<Window x:Class="ABC.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:ABC"
        Title="MainWindow" Height="350" Width="525">
    <Grid Margin="10">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="10"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="10"/>
        </Grid.RowDefinitions>

        <!-- FOLLOWING LINE CAUSING ERROR-->
        <ContentControl Name="Toolbar" Content="{StaticResource View1 }"></ContentControl>

    </Grid>
</Window>

What am I missing?

Thanks, Amit

4
  • 1
    Can you please show us where you actually reference the control? There is not enough information here to propose a solution. Commented Aug 14, 2012 at 17:13
  • You need to specify the actual resource dictionary in your MainWindow.xaml Commented Aug 14, 2012 at 17:23
  • @PiotrPtak what if I don't want to mix it with my MainWindow.xaml? Commented Aug 14, 2012 at 17:27
  • "Why WPF is not very good when it comes to referencing?": why do you assume it is WPF that is "not very good"? Perhaps you're the one who's not very good when it comes to WPF ;). Don't blame something just because you don't understand it properly... Commented Aug 14, 2012 at 17:46

2 Answers 2

4

Since View1 is referenced in a separate ResourceDictionary you need to merge it into the ResourceDictionary for this Window.

Try adding this to the code just inside your Window declaration:

<Window.Resources>
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="Resource/Views/ApplicationToolbar.xaml"/>
  </ResourceDictionary.MergedDictionaries>
</Window.Resources>

At this point your Window should be able to reference View1.

Note: I haven't tested this fully in an IDE, so it's possible there could be a slight syntax error or issue with the path. You may have to format your dictionary URL with a Pack URI in order for the reference to resolve correctly. Resource paths in WPF tend to be a little tricky.

Sign up to request clarification or add additional context in comments.

Comments

0

Thanks to all. OK, I tried what was suggested here and I appreciate Mike and it was close to the solution. What worked for me was adding following tag in App.xaml:

<Application.Resources>
    <ResourceDictionary Source="Resources/views/ApplicationToolbar.xaml"/>
</Application.Resources>

Thanks. Amit

Comments

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.