0

After a couple of hours searching the web, I turn to you all:

My WPF class library (.NET 3.5, COM visible) has a form in it, which uses an UserControl and a theme file.

The problem: When using WPF Applications, the buttons work just fine, but in this .NET 3.5, COM Visible, class library, they won't show at all. Other objects from the library can be used and work. What could be the problem? I'm leaning towards the resource dictionary that can't be found, or some of the resources that cannot be found, but I can't put my finger on it. Any help would be most welcome!

Some code

The resources are set via the

<Window.Resources>
   <ResourceDictionary>
       <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary  Source="pack://application:,,,/MyLibrary;component/Themes/Generic.xaml"/>
       </ResourceDictionary.MergedDictionaries>
   </ResourceDictionary>
</Window.Resources>

There we place - for now - a simple:

<Grid>
   <lib:ImageButton ImageSource="/MyLibrary;component/Images/some_image.png" />
</Grid>

Not forgetting the reference for the library in the project and the xamls:

xmlns:lib="clr-namespace:MyLibrary;assembly=MyLibrary"

The MyLibrary holds this ImageButton - a simple button extension mainly to hold an image. The WPF looks somewhat like:

<UserControl x:Class="MyLibrary.ImageButton"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:lib="clr-namespace:MyLibrary"
         x:Name="me"
         Width="auto"
         Height="22"
         HorizontalAlignment="Center" VerticalAlignment="Center">

<UserControl.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type library:ImageButton}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                    <Setter Property="Width" Value="22" />
                </DataTrigger>
                <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                    <Setter Property="Width" Value="auto" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ResourceDictionary>
</UserControl.Resources>

<Button x:Name="_button" Click="Button_Click" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="auto">
    <StackPanel Orientation="Horizontal">
        <Image Source="{Binding ElementName=me, Path=ImageSource}" Stretch="Uniform" />
        <TextBlock x:Name="_Text" Text="{Binding ElementName=me, Path=Text}" VerticalAlignment="Center" Margin="2, 0, 0, 0">
            <TextBlock.Resources>
                <Style TargetType="{x:Type TextBlock}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="False">
                            <Setter Property="Visibility" Value="Collapsed" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding ElementName=me, Path=HasText}" Value="True">
                            <Setter Property="Visibility" Value="Visible" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Resources>
        </TextBlock>
    </StackPanel>
</Button>
</UserControl>

The .cs side is fairly arbitrary I guess.

Finally, we have Generic.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/MyLibrary;component/Themes/MyTheme.xaml" />
    </ResourceDictionary.MergedDictionaries>

</ResourceDictionary>

Where "MyTheme" holds lots of templates, colours etc..

Knots
  • 551
  • 8
  • 19
  • Have you set the ThemeInfoAttribute for your assembly? WPF can be a bit troublesome with themes in class libraries. – Chris Oct 14 '13 at 15:48
  • Could it be your ImageButton.ImageSource uri being confused by pointing to another assembly ? And since your grid does not have a minimum size, it might be in your app. but not visible to you. Try a 'pack' syntax instead : http://msdn.microsoft.com/en-us/library/aa970069.aspx. Also, double-check with Snoop whether that control is in your visual tree : http://snoopwpf.codeplex.com/ – aybe Oct 14 '13 at 17:26
  • The ThemeInfo is set indeed. All other UserControls, which use the same theme, work fine. It's just the ImageButton that won't show. The pack syntax won't do the job either. Snoop does find the ImageButton: the base uri is correct, though the UserControls content (A Button, which again holds an Image) has a width/height of NaN, and no background / border colour. Is should however grow to fit the image set into it. Any ideas? – Knots Oct 15 '13 at 08:19

2 Answers2

1

The problem most likely stems from the fact that your Class Library project has no idea that there are resource dictionaries you want to consume everywhere even though you merged your resource dictionaries in some file. The reason for this is that Visual Studio and Blend both look for an ApplicationDefinition file for global resource dictionaries to consume.

Class Library projects cannot have ApplicationDefinition files. However, there is a way you can share resources for design time viewing of your custom control project. A question on that topic and the answer can be found here (disclaimer: the link currently points to my answer).

Please take note that if you use the solution linked to above, you will still have to reference your resource dictionaries at the App.xaml level of any Application project that consumes your class library as your class library will not contain the specific styling..

JoshuaTheMiller
  • 2,582
  • 25
  • 27
1

Well this question is rather old but here goes:

After doing a lot of comparison i verified that the way to get it working is setting the following lines somewhere in your class library on the assembly level:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, 
    ResourceDictionaryLocation.SourceAssembly 
)]
Dbl
  • 5,634
  • 3
  • 41
  • 66