0

I have some styles and templates in App.Xaml so I can acces them thru multiple UserControls.

EDIT : This is within the app.Xaml:

One of the Styles are :

<Application x:Class="BaseRefence.generatingAnnotation.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:BaseRefence.generatingAnnotation"
             xmlns:Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero2"
             StartupUri="MainWindow.xaml">
<Application.Resources>
<Style x:Key="ComboBoxStyleRounded" TargetType="{x:Type ComboBox}">
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="{x:Type TextBox}">
            <Grid>
               <Border CornerRadius="25"
                       BorderThickness="1,1,2,2">
               </Border>
            </Grid>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
   <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
   <Setter Property="Background" Value="{StaticResource ComboBox.Static.Background}"/>
   <Setter Property="BorderBrush" Value="#42536b"/>
   <Setter Property="Foreground" Value="Black"/>
   <Setter Property="BorderThickness" Value="1,1,2,2"/>
   <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto"/>
   <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
   <Setter Property="Padding" Value="6,3,5,3"/>
   <Setter Property="ScrollViewer.CanContentScroll" Value="true"/>
   <Setter Property="ScrollViewer.PanningMode" Value="Both"/>
   <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
   <Setter Property="Template" Value="{StaticResource ComboBoxTemplate}"/>
   <Style.Triggers>
      <Trigger Property="IsEditable" Value="true">
         <Setter Property="IsTabStop" Value="false"/>
         <Setter Property="Padding" Value="2"/>
         <Setter Property="Template" Value="{StaticResource ComboBoxEditableTemplate}"/>
      </Trigger>
   </Style.Triggers>
</Style>
</Application.Resources>
</Application>

And in de UserControl.Xaml I have:

<ComboBox ItemsSource="{Binding ViewFamilyTypesInProject}"                                                              
          SelectedItem="{Binding SelectedViewFamilyType, Mode=TwoWay}"
          Grid.Row="1"
          Grid.Column="1"
          Margin="10 5"
          MaxHeight="40"
          Style="{DynamicResource ComboBoxStyleRounded}">

Within the designer everything works great, it shows correctly and all. However, whenever I build and run my code, it gives the message and it does not override the style but keeps the default style.

System.Windows.ResourceDictionary Warning: 9 : Resource not found; ResourceKey='ComboBoxStyleRounded'
Stefan
  • 121
  • 2
  • 12
  • Can you access the other templates / styles ? – Niklas Jul 14 '20 at 09:23
  • why dynamic? did you try StaticResource? – Bizhan Jul 14 '20 at 09:33
  • None of the created Styles are accessible. I just did the edit template -> make a copy. and moved that to Application.resources instead of UserControl.resources – Stefan Jul 14 '20 at 09:34
  • Yes I tried Static, but then the total app crashes when I try to load the usercontrol – Stefan Jul 14 '20 at 09:35
  • Are the UserControl and App.xaml in the same project? Can you add the App.xaml? (with just the ComboBox style) – Niklas Jul 14 '20 at 09:49
  • Yes they are in the same project. within the same namespace. The top part of the question is within the App.Xaml. – Stefan Jul 14 '20 at 09:54
  • Try to wrap the styles in a ResourceDictionary. ` ` ... – Niklas Jul 14 '20 at 09:59
  • This did not work, do I need to adress the style bind differently when I Wrap it in a ResourceDictionary? the Odd thing is that within my designer it works Fine – Stefan Jul 14 '20 at 10:10
  • Move the ComboBox style to the bottom of the App.XAML. It’s a known “bug”. – Legacy Code Jul 14 '20 at 10:21
  • That didn't work, beside, it's with every style I made. I just created a new sample project, within that it works just fine, wich makes it even more odd for me – Stefan Jul 14 '20 at 10:29
  • use a static resource and try to put the style in a dictionary like in this answer: https://stackoverflow.com/a/22114430/366064 also you might want to rename the style, maybe it's defined elsewhere – Bizhan Jul 14 '20 at 11:43
  • If everything should work then I would suggest deleting `obj` folder in your solution. Then clean project and then build it. See if that helps, looks like there might be some leftover artefacts from precious builds. – XAMlMAX Jul 14 '20 at 12:33

2 Answers2

2

My experience: I frequently have same issues (using VS 16.6.2, .NET Core 3.1 and WPF).

Simply close Visual Studio and restart it: 98% of the times error disappears (will reappear sometime in the future).

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
0

You have an invalid Style. It may compile, as the XAML is syntactically correct, but the semantics of the markup are wrong. Since you are referencing the Style using DynamicResource the error occurs at runtime. But I wonder why don't get a XAML Designer error.

You are setting the ComboBox.Template property twice. Moreover and most important, the first ControlTemplate at the top of the Style is targeting the wrong type TextBox:

<ControlTemplate TargetType="{x:Type TextBox}">
  <Grid>
    <Border CornerRadius="25"
            BorderThickness="1,1,2,2">
    </Border>
  </Grid>
</ControlTemplate>

The type must be of course <ControlTemplate TargetType="ComboBox">.

Since you are referencing a ControlTemplate resource later

<Setter Property="Template" Value="{StaticResource ComboBoxTemplate}" />

I think you want to remove the first ControlTemplate. If you want to change the appearance of the TextBox, you would need to override the complete ControlTemplate of the ComboBox.
You may should run Clean Solution and Rebuild Solution.

BionicCode
  • 1
  • 4
  • 28
  • 44