I'm developing a VS extension and I want to achieve that my UI will use colors (font, background etc.) depending on the selected VS-color-scheme. What is the best way to do this. Can I bind against some static ressources in my WPF?
Yes, binding to static VS resources is the best approach. It is supported in VS 2012+ and looks like this:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vs_shell="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Shell.11.0">
<Style TargetType="Label">
<Setter Property="Foreground" Value="{DynamicResource {x:Static vs_shell:EnvironmentColors.ToolWindowTextBrushKey}}"/>
</Style>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="{DynamicResource {x:Static vs_shell:EnvironmentColors.ToolWindowTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static vs_shell:EnvironmentColors.ToolWindowBackgroundBrushKey}}"/>
</Style>
</ResourceDictionary>
See EnvironmentColors Class for all avilable colors.
Related
My English skill is poor because I'm not a native English speaker.
I hope you to understand.
I trying to create the style of control to support the skin.
Recently, I created a style for ComboBox as shown below.
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="{DynamicResource ActiveTextBrush}"/>
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource InActiveTextBrush}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="true">
<Setter Property="Foreground" Value="{DynamicResource ActiveTextBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
The above code shows the result of the below.
Unfortunately, the result is not what I want.
I wanted painting the Background color is to Black but the above code doesn't change Background color.
What I should do to achieve my goal?
Thank you for reading.
Microsoft offers their generic styles and templates for all their core components in WPF framework directly in documentation.
Here is the link for ComboBox styles and templates.
You can grab the XAML code from there (including the common resources at the bottom), adapt it as needed in your app, and add it to your own resources, e.g. App.xaml's Resources.
(Using Blend for Visual Studio or Visual Studio's own extract style feature, are other options, but in my experience, I found that sometimes these have some quirks/issues with the generated XAML.)
we are using telerik control in our WPF application. I am applying custom style for telerik control but it is not working.
<Style x:Key="RadButtonStyle" TargetType="telerik:RadButton">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Margin" Value="3,3,5,3"/>
</Style>
<telerik:RadButton Style="{StaticResource RadButtonStyle}"
Content="Login"
Margin="5"
cmd:Click.Command="
{Binding LoginCommand}" />
but style is not getting applied. it is actually hiding the button...
What I did: I have downloaded NugetPackage for Telerik Theme and added reference
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/>
<ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.GridView.xaml"/>
Try to base your Style on the implicit one using the BasedOn property:
<Style x:Key="RadButtonStyle" TargetType="telerik:RadButton"
BasedOn="{StaticResource {x:Type telerik:RadButton}}">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Width" Value="60"/>
<Setter Property="Height" Value="22"/>
<Setter Property="Margin" Value="3,3,5,3"/>
</Style>
I'm trying to figure out how to change the style of the AvalonEdit CodeCompletion window. However, I can't figure out the right combination of xaml style target/properties to change it. The main thing I'd like to do is get rid of the border, but maybe some additional changes as well.
Here is the xaml I've tried. None of it is affecting the UI.
xmlns:ae="clr-namespace:ICSharpCode.AvalonEdit.CodeCompletion;assembly=ICSharpCode.AvalonEdit"
<Style TargetType="{x:Type ae:CompletionWindow}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionWindowBase}">
<Setter Property="WindowStyle" Value="None" />
</Style>
<Style TargetType="{x:Type ae:CompletionListBox}">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="{x:Type ae:CompletionList}">
<Setter Property="Background" Value="Orange" />
</Style>
Use this style to remove border on window:
<Style TargetType="{x:Type avalonEdit:CompletionWindow}">
<Setter Property="WindowStyle" Value="None"></Setter>
<Setter Property="ResizeMode" Value="NoResize"></Setter>
<Setter Property="BorderThickness" Value="0"></Setter>
</Style>
To make the styles affect the UI, you can put them in a resource dictionary xaml and parse that with (ResourceDictionary)XamlReader.Parse(ResourcesAsXaml).
Then assign the ResourceDictionary to the Resources property of the CompletionWindow.
I am new to window phone 8 programming. I watched Channel 9's tutorial called Windows Phone 8 Development for Absolute Beginners, and now I am learning to write some simple code.
Could anyone explain the relationship between the "Style" and the "Grid.Resources" in the sample code below? I though that an inner tag, ie. Style, is a property of it's outer tag, ie. Grid. But it doesn't make sense here since the outer tag, Grid.Resource, is actually a property.
<Grid.Resources>
<Style TargetType="Border">
<Setter Property="BorderBrush" Value="Gold" />
<Setter Property="BorderThickness" Value="2" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Padding" Value="5" />
</Style>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black" />
</Style>
</Grid.Resources>
Any help would be appreciated.
TargetType refers the System.Windows.Controls.Border MSDN which is a class
Property refers to the property of the Border class.
In other words Style can only be based on a class, but that class can be a Property in other class like so:
ListView->View(GridView)
And your Style
<Style x:Key="sampleStyle" x:TargetType="GridView">
<Setter Property="Background" Value="Red"/>
</Setter>
I want to set different backgrounds for GridSplitter's who are horizontal vs vertical. This is because I have a linear gradient and I need to rotate it 90deg depending on the alignment of the grid splitter.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type GridSplitter}">
<Setter Property="Background" Value="Red" /> <!-- How to get this red applied to only Vertical for instance? -->
</Style>
</ResourceDictionary>
So the question is: how do I target vertical splitters and horizontal splitters separately?
Use Style.Triggers to apply setters conditionally.
Okay looks like I got it:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type GridSplitter}">
<Style.Triggers>
<Trigger Property="VerticalAlignment" Value="Stretch">
<Setter Property="Background" Value="#F7F7F7" />
</Trigger>
<Trigger Property="HorizontalAlignment" Value="Stretch">
<Setter Property="Background" Value="red" />
</Trigger>
</Style.Triggers>
</Style>
</ResourceDictionary>