locked
How to have Visual Studio Extension's controls extend the VS theme RRS feed

  • Question

  • I've built a Visual Studio Extension called Diff All Files.  This extension adds a new section to the Team Explorer's Pending Changes window (as well as others), and has some buttons, labels, and combo boxes on it.  The controls look fine if the default Light theme, but don't switch colors when the user changes to the Dark theme, so they stand out like a sore thumb.

    I found this post which mentions that if you don't apply a style to your controls, they should automatically inherit the theme's style, and they do.  My problem is that I want to apply additional style attributes on my controls to adjust the Margins, length of time that ToolTips are displayed for, etc.  So I have this in my User Control's xaml:

    <UserControl.Resources>
    	<Style TargetType="{x:Type Button}">
    		<Setter Property="ToolTipService.ShowDuration" Value="60000" />
    		<Setter Property="Padding" Value="5,2,5,2" />
    		<Setter Property="Margin" Value="0,2,5,2" />
    		<Setter Property="Height" Value="25" />
    		<Setter Property="VerticalAlignment" Value="Center" />
    	</Style>
    	...
    </UserControl.Resources>

    If I get rid of this UserControl.Resources section then the theme's colors are applied to my controls, but I don't want to do this because it means I would have to manually specify these additional attributes on every single Button that I define.  I was hoping there would be away to simply build off of the inherited style.

    I found this Stack Overflow post asking the same question, but their given answer of using the BasedOn property doesn't seem to work for my case.  If I change my line above to the following, it still completely overwrites the VS theme's style:

    <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">

    Any ideas on how I can simply extend the Visual Studio theme's style for my controls?

    Thanks


    - Dan - "Can't never could do anything"


    • Edited by deadlydog Wednesday, May 21, 2014 10:56 PM
    Wednesday, May 21, 2014 10:55 PM

Answers

  • Hi Vicky, thanks for the reply.  I took a look at their solution and it looks much to complicated for my simple extension.  I've decided to compromise a bit and take a different approach.  Instead of creating a new Style and assigning it a Target Type, I've created some "class variables" in the xaml, and then reference them from a control's attributes:

    xmlns:System="clr-namespace:System;assembly=mscorlib"
    

    ...

    <UserControl.Resources> <!-- We can't define styles for each control type (e.g. Button) because when we do, it overrides the inherited style, so our controls won't match the current Visual Studio theme; and using the BasedOn attribute doesn't seem to work. So we have to manually apply these to every control :( --> <System:Int32 x:Key="ToolTipServerShowDuration">60000</System:Int32> <Thickness x:Key="ButtonPadding">5,2,5,2</Thickness> <Thickness x:Key="ButtonMargin">0,2,5,2</Thickness> <System:Double x:Key="ButtonHeight">25</System:Double> <VerticalAlignment x:Key="ButtonVerticalAlignment">Center</VerticalAlignment> ... </UserControl.Resources> ... <Button Content="All Files" IsEnabled="{Binding Path=IsCompareAllFilesEnabled}" Click="btnDiffAllFiles_Click" ToolTip="Compare all files." ToolTipService.ShowDuration="{StaticResource ToolTipServerShowDuration}" Padding="{StaticResource ButtonPadding}" Margin="{StaticResource ButtonMargin}" Height="{StaticResource ButtonHeight}" VerticalAlignment="{StaticResource ButtonVerticalAlignment}" /> <!--<Button Content="Selected" IsEnabled="{Binding Path=IsCompareSelectedFilesEnabled}" Click="btnDiffSelectedFiles_Click" ToolTip="Compare only selected files." />--> <Button Content="Included" IsEnabled="{Binding Path=IsCompareIncludedFilesEnabled}" Visibility="{QC:Binding '$P ? Visibility.Visible : Visibility.Collapsed', P={Binding Path=IsCompareIncludedFilesAvailable}}" Click="btnDiffIncludedFiles_Click" ToolTip="Compare only included files." ToolTipService.ShowDuration="{StaticResource ToolTipServerShowDuration}" Padding="{StaticResource ButtonPadding}" Margin="{StaticResource ButtonMargin}" Height="{StaticResource ButtonHeight}" VerticalAlignment="{StaticResource ButtonVerticalAlignment}" />

    So you will notice that I still have to add the ToolTipService, Padding, Margin, Height, and Vertical Alignment attributes to every Button control, but I use bindings to grab their values from the resources that I defined.  So it sucks that I still have to add the attributes to every control, but at least I can still change them all by only having to modify one value in the Resources section.

    I hope this helps others who come across this problem in the future too.  It's really too bad that simply defining a style and using the BasedOn attribute doesn't work for VS themes.


    - Dan - "Can't never could do anything"


    • Edited by deadlydog Thursday, May 29, 2014 4:16 PM
    • Marked as answer by deadlydog Thursday, May 29, 2014 4:17 PM
    Thursday, May 29, 2014 4:16 PM

All replies

  • Hi,

    I'm trying to involve some senior engineers into this issue and it will take some time. Your patience will be greatly appreciated.

    We will let you know if any progress is made.

    Sorry for any inconvenience.

    Best regards,

    Thursday, May 22, 2014 9:56 AM
  • Hi,

    How about using VsBrushes and VsColors classes? See Mikhail's reply in this link for the detailed information: http://stackoverflow.com/questions/17345601/standard-controls-in-vs-2012-extension

    Thanks,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, May 23, 2014 2:05 AM
  • Hi Vicky, thanks for the reply.  I took a look at their solution and it looks much to complicated for my simple extension.  I've decided to compromise a bit and take a different approach.  Instead of creating a new Style and assigning it a Target Type, I've created some "class variables" in the xaml, and then reference them from a control's attributes:

    xmlns:System="clr-namespace:System;assembly=mscorlib"
    

    ...

    <UserControl.Resources> <!-- We can't define styles for each control type (e.g. Button) because when we do, it overrides the inherited style, so our controls won't match the current Visual Studio theme; and using the BasedOn attribute doesn't seem to work. So we have to manually apply these to every control :( --> <System:Int32 x:Key="ToolTipServerShowDuration">60000</System:Int32> <Thickness x:Key="ButtonPadding">5,2,5,2</Thickness> <Thickness x:Key="ButtonMargin">0,2,5,2</Thickness> <System:Double x:Key="ButtonHeight">25</System:Double> <VerticalAlignment x:Key="ButtonVerticalAlignment">Center</VerticalAlignment> ... </UserControl.Resources> ... <Button Content="All Files" IsEnabled="{Binding Path=IsCompareAllFilesEnabled}" Click="btnDiffAllFiles_Click" ToolTip="Compare all files." ToolTipService.ShowDuration="{StaticResource ToolTipServerShowDuration}" Padding="{StaticResource ButtonPadding}" Margin="{StaticResource ButtonMargin}" Height="{StaticResource ButtonHeight}" VerticalAlignment="{StaticResource ButtonVerticalAlignment}" /> <!--<Button Content="Selected" IsEnabled="{Binding Path=IsCompareSelectedFilesEnabled}" Click="btnDiffSelectedFiles_Click" ToolTip="Compare only selected files." />--> <Button Content="Included" IsEnabled="{Binding Path=IsCompareIncludedFilesEnabled}" Visibility="{QC:Binding '$P ? Visibility.Visible : Visibility.Collapsed', P={Binding Path=IsCompareIncludedFilesAvailable}}" Click="btnDiffIncludedFiles_Click" ToolTip="Compare only included files." ToolTipService.ShowDuration="{StaticResource ToolTipServerShowDuration}" Padding="{StaticResource ButtonPadding}" Margin="{StaticResource ButtonMargin}" Height="{StaticResource ButtonHeight}" VerticalAlignment="{StaticResource ButtonVerticalAlignment}" />

    So you will notice that I still have to add the ToolTipService, Padding, Margin, Height, and Vertical Alignment attributes to every Button control, but I use bindings to grab their values from the resources that I defined.  So it sucks that I still have to add the attributes to every control, but at least I can still change them all by only having to modify one value in the Resources section.

    I hope this helps others who come across this problem in the future too.  It's really too bad that simply defining a style and using the BasedOn attribute doesn't work for VS themes.


    - Dan - "Can't never could do anything"


    • Edited by deadlydog Thursday, May 29, 2014 4:16 PM
    • Marked as answer by deadlydog Thursday, May 29, 2014 4:17 PM
    Thursday, May 29, 2014 4:16 PM