tags:

views:

141

answers:

1

I have a UserControl with a Border element within it that I want to style with a particular Border style. It compiles but won't start, giving a XamlParseException, saying, "Cannot find resource ..."

Is there a way to do this?

Thanks.

App.xaml:

<cal:CaliburnApplication x:Class="WahnamProgressTracker.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:Converters="clr-namespace:WahnamProgressTracker.Converters;assembly=WahnamProgressTracker"
xmlns:Model="clr-namespace:WahnamProgressTracker.Model">
<Application.Resources>
    <Style x:Key="FancyBorder"
           TargetType="{x:Type Border}">
        <Setter Property="Margin" Value="0,0,0,8"/>
        <Setter Property="Padding" Value="8"/>
        ...
    </Style>
</Application.Resources>

MainView.xaml:

<Window x:Class="WahnamProgressTracker.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cal="http://www.caliburnproject.org"
xmlns:uc="clr-namespace:WahnamProgressTracker.UserControls"
MinHeight="500" MinWidth="800">

<DockPanel>
    <uc:MainViewMenu x:Name="menu"
                     DockPanel.Dock="Top" />

    <StatusBar x:Name="quoteBar"                   
               DockPanel.Dock="Bottom">
        <TextBlock Text="{Binding Path=Quote.Text, Mode=OneWay}" />
    </StatusBar>

    <uc:MainViewNavigation x:Name="navigationBar"
                           DockPanel.Dock="Left" />

    <uc:ProgressGraph x:Name="graph" />
</DockPanel>

MainViewNavigation.xaml (user control):

<UserControl x:Class="WahnamProgressTracker.UserControls.MainViewNavigation"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;

    <Border Style="{StaticResource FancyBorder}">
        ...       
    </Border>    
</UserControl>
+2  A: 

Can you post a sample of what you mean? The only case in which your issue can occur is if the User Control is created and then rendered outside your application's visual tree.

The XAML below works for me:

App.xaml:

<Application x:Class="WpfApplication1.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="Window1.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type TextBlock}" x:Key="myStyle">
            <Setter Property="Foreground" Value="Green" />
            <Setter Property="FontWeight" Value="Bold" />
        </Style>
    </Application.Resources>
</Application>

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <local:UserControl1 />
    </Grid>
</Window>

UserControl1.xaml:

<UserControl x:Class="WpfApplication1.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="300" Width="300">
    <Grid>
        <TextBlock Style="{StaticResource myStyle}">HEY!</TextBlock>
    </Grid>
</UserControl>
siz
hmmm... could it be that my user control is in a different namespace? it would be "x:Class="WpfApplication1.UserControls.UserControl1""...
Alex Baranosky
No, that should not matter. I recommend, if possible, that you add some code to your question. It will be easier to diagnose that way.
siz
one sec .
Alex Baranosky
For some reason SO is cutting the final line off of the first two code segments I posted, but rest assured they are in the code
Alex Baranosky
Hey siz, did you forget this question? I still haven't figured it out (been working around it)
Alex Baranosky