Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I reliably determine if a `ControlTemplate` is being used in a WPF application?

How do I reliably determine if the following ControlTemplate is being used in a WPF application? The file name is 'CheckBoxTemplates.xaml' and is in a different assembly than the main application. Note, there were no results when I searched for the file name and the resource key. In addition, searching the solution for a resource-key is not reliable. Especially, when there is five resource dictionary files that contain the same key.

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">
    <Style x:Key="invertedCheckBox"
           TargetType="CheckBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="CheckBox"
...

More Information

While user2250152 answser is correct for the above XAML. It does not reliably determine if a style is in use. I say this because, when I used the technique for another style, I found the five resource dictionary files that contain the same key. Therefore, we have to consider how do I determine reliably which style is used with defined with duplicate keys.

like image 493
AMissico Avatar asked Dec 23 '14 20:12

AMissico


4 Answers

ReSharper shows dead code. Although its detection algorithm isn't perfect, it is a good start, allowing you to skip any style which it shows as used. For any style which is shown as unused, you can then check if it actually is unused (ex. by searching through the solution).

like image 70
Daniel Rose Avatar answered Nov 02 '22 17:11

Daniel Rose


There are a few options I can think of:

  • The 'development time' solution: this includes searching every file for the existence of Style="{StaticResource StyleName}", or something similar. This will work in a lot of cases, but isn't automated and not very reliable;

  • The runtime solution: You could instantiate every control in the class library, and iterate over every element in it, checking the currently assigned Style. This only works for styles being set immediately, and not during events, etc.;

  • The 'code' solution: You could create your own facade static class, that is an entry point to get a style. Note you are not allowed to use Style="{StaticResource StyleName}" any more, but Style="{StaticResource SomeStaticClass.StyleName}". This will only work when you have a quality development team that is used to this working method. You can count the number of times a style is accessed over the duration of the program. If after a lot of time, the Style hasn't been used yet, it is probably candidate to get rid of.

like image 23
Patrick Hofman Avatar answered Nov 02 '22 17:11

Patrick Hofman


Unorthodox way of getting there would be to create an attached property which prints out who is using the ControlTemplate :) think about it like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna">
    <Style x:Key="invertedCheckBox"
           TargetType="CheckBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate  TargetType="CheckBox">
                 <Border my:WhoIsMyDaddy="true" ...>

my:WhoIsMyDaddy could be an attached property which triggers propertychanged event handler on initalizing when set to "true". Once handler called you will be able to access the instance of the control which is the owner of the attached property. In the end just print out whatever you want using that instance.

like image 21
dev hedgehog Avatar answered Nov 02 '22 15:11

dev hedgehog


You can use VisualStudio and "Find in files" tool. Try to find style key, for example invertedCheckBox in entire solution. So you can determine where the style is used.

like image 33
user2250152 Avatar answered Nov 02 '22 15:11

user2250152