1

I'm trying to create custom control for my WPF application and cant understand why its not working.

I have two projects - one is my custom control library with one custom control derived from Image with only few methods and the second project where I want to use my control. I dont want to do anything with styles or binding, so I'm using defaults.

So, my custom control theme (generic.xaml):

   <ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:RTPlayer">
    <Style 
     TargetType="{x:Type local:RTPlayer}" BasedOn="{StaticResource {x:Type Image}}">
    </Style>

And code part:

namespace RTPlayer
{
   public class RTPlayer : Image
   {
    static RTPlayer()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(RTPlayer), new FrameworkPropertyMetadata(typeof(RTPlayer)));
    }

    public void Start()
    {
       // ....
    }
   }
}

In my main project I have added reference to my library .dll file and I'm trying to use control as:

  <Window x:Class="rtspWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:rtspWPF"
    xmlns:CustomControls="clr-namespace:RTPlayer;assembly=RTPlayer"
    xmlns:CustomControls="clr-namespace:RTPlayer;assembly=RTPlayer"
    Title="MainWindow" Height="auto" Width="auto">
<CustomControls:RTPlayer x:Name="image" Margin="10,10,10,10" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Width="auto" Height="auto"/>

The problems is - 1) xaml CustomControl:RTPlayer string warns me: "cant find resource named "System.Windows.Controls.Image". Resource names are case sensitive"; 2) If I'm trying to launch app its throw a Markup.XamlParseException that Markup.StaticResourceHolder throw exception..

What is wrong with my code?

2
  • Can you post the stack trace of XamlParseException.InnerException? Commented Oct 10, 2016 at 15:47
  • 1
    The Image Control doesn't have a base style you can inherit from in your Style. Commented Oct 10, 2016 at 18:27

1 Answer 1

1

Thanks to Daniel answer in comments, I have found out what I should do to make my app work.

After Daniel told me that Image component doest not have a base style, I found a guy with simillar issue: How to inherit type-based styles in WPF?

Than I just rewrited my Generic.xaml file to this:

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

   <Style TargetType="Image" x:Key="ImageStyle">

   </Style>

   <Style TargetType="{x:Type local:RTPlayer}" BasedOn="{StaticResource ImageStyle}">
   </Style>
</ResourceDictionary>

And everything worked!

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

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.