2009-08-27 6 views
1

J'ai un UserControl avec un élément Border que je veux styliser avec un style Border particulier. Il compile mais ne démarre pas, en donnant une exception XamlParseException, en disant: "Impossible de trouver la ressource ..."Comment laisser mes UserControls utiliser les mêmes styles que App.xaml?

Y at-il un moyen de faire cela?

Merci.

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 (contrôle de l'utilisateur):

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

    <Border Style="{StaticResource FancyBorder}"> 
     ...  
    </Border>  
</UserControl> 

Répondre

2

Pouvez-vous poster un échantillon de ce que vous voulez dire? Le seul cas dans lequel votre problème peut se produire est si le contrôle utilisateur est créé et rendu en dehors de l'arborescence visuelle de votre application.

Le XAML ci-dessous fonctionne pour moi:

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

hmmm .. pourrait-il être que mon contrôle de l'utilisateur est dans un espace de noms différent? ce serait "x: Class =" WpfApplication1.UserControls.UserControl1 "" ... –

+0

Non, cela ne devrait pas avoir d'importance. Je recommande, si possible, que vous ajoutiez du code à votre question. Ce sera plus facile de diagnostiquer de cette façon. –

+0

une seconde. –

Questions connexes