简体   繁体   中英

property Template not found for custom wpf control

I am trying to modify the wpf tray icon at http://www.hardcodet.net/projects/wpf-notifyicon

the aim is to have a separate xaml file that would define a trayicon with a specific configuration, so that this configuration, in a separate file, could be easily added to say a wpf window.

im very new to wpf, but am trying to use ControlTemplate in a ResourceDictionary to achieve the aim:

Pointing to resource in App.xaml

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="trayicon.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

The separated configuration of trayicon.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification">
<ControlTemplate x:Key="TrayIcon" TargetType="{x:Type tb:TaskbarIcon}">
            <tb:TaskbarIcon x:Name="MyNotifyIcon"
        IconSource="/TaskbarNotification/DefaultTrayIcon.ico"
        ToolTipText="I am notified yes!"
        MenuActivation="LeftOrRightClick"></tb:TaskbarIcon>
</ControlTemplate>
</ResourceDictionary>

Trying to use the configuration

<Window x:Class="Test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
    xmlns:tb="clr-namespace:Hardcodet.Wpf.TaskbarNotification"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <tb:TaskbarIcon Template="{StaticResource TrayIcon}"></tb:TaskbarIcon>
</Grid>
</Window>

Exception thrown in MainWindow.xaml

The property 'Template' was not found in type 'TaskbarIcon'

My guess is that i have to add some code to this custom wpf control that woudl expose the Template property and set it how it should be set? But i have no idea how i would do that, could you point me in the right direction please?

Wild partial guess of code needed

public static readonly DependencyProperty TemplateProperty =
    DependencyProperty.Register("Template",
                                typeof (SomeTemplateType),
                                typeof (TaskbarIcon),
                                new FrameworkPropertyMetadata(SomeTemplateType.Empty, TemplatePropertyChanged));

[Category(CategoryName)]
[Description("Enables Templating.")]
public SomeTemplateType Template
{
  get { return (SomeTemplateType)TemplateProperty; }
  set { SetValue(TemplateProperty,value); }
}

Cel I dont understand the template in the first place. The control template is targetted for tb:TaskbarIcon which actually has another tb:TaskbarIcon inside it!!!

I assume you want following properties to be applied to TrayIcons across your application with their specified values...

     IconSource="/TaskbarNotification/DefaultTrayIcon.ico";
     ToolTipText="I am notified yes!";
     MenuActivation="LeftOrRightClick"

If thats so then assuming that above properties are dependency properties, instead of creating a control template why dont you create a style which is targetted to tb:TaskbarIcon and specify Setters which set the above properties with their corresponding values.

   <Style x:Name="MyNotifyIcon" TargetType="{x:Type tb:TaskbarIcon}">
        <Setter Property="IconSource" Value="/TaskbarNotification/DefaultTrayIcon.ico"/>
        <Setter Property="ToolTipText" Value="I am notified yes!" />
        <Setter Property="MenuActivation" Value="LeftOrRightClick" />
  </Style>

Then apply this style to your TaskbarIcon

  <tb:TaskbarIcon Style="{StaticResource MyNotifyIcon}"></tb:TaskbarIcon>

So basically if this is what you are looking for, then template is out of question. Please suggest if this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM