How to apply style for WPF DATAGRID Scrollbars - wpfdatagrid

I have a WPF Datagrid control. I want to apply style for scrollbars. It would be nice if u can give me an example for it.
Thanx

How about something this?
<DataGrid>
<DataGrid.Resources>
<Style TargetType="ScrollBar">
<Setter Property="Background" Value="Red" />
</Style>
</DataGrid.Resources>
</DataGrid>

Related

How do I base a style on the default style in Windows Universal apps (Win10)?

In WPF, if you want to base a style on the default style of a control, you would say:
<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource {x:Type GridView}}">
However, x:Type is not supported on UAP - how do I do it then? I tried the following - none works (after defining XAML as an alias for the namespace where GridView is).
<Style TargetType="customControls:ResponsiveGridView" BasedOn="{StaticResource xaml:GridView}">
<Style TargetType="customControls:ResponsiveGridView" BasedOn="xaml:GridView">
None of this works - crashes on parsing the XAML.
Any more ideas?
You can still use "BasedOn" for inheritance of styles.
<Page.Resources>
<Style TargetType="Button" x:Key="MyOtherStyle">
<Setter Property="Background" Value="Red"></Setter>
</Style>
<Style TargetType="Button" BasedOn="{StaticResource MyOtherStyle}" >
<Setter Value="Green" Property="Foreground"></Setter>
</Style>
</Page.Resources>
Just define the resources like above. They'll be applied for every button on the page.
<Button Content="Hello"></Button>
To base on a default style of a control, you don't use "BasedOn". You implicitly base on the default style of a control by specifying the TargetType in the style.
To be more precise for your special case:
If you want to use an (implicit) style for your custom control that is based on a default style of a built-in control do the follwing:
Create a custom style that targets the built-in control type. Like this:
<Page.Resources>
<Style TargetType="Grid" x:Key="MyStyle1" >
<Setter Property="Background" Value="Green"></Setter>
</Style>
...
Then add another style that targets your custom control type an is based on your custom style for the built-in control. Like this:
...
<Style TargetType="local:MyCustomGrid" BasedOn="{StaticResource MyStyle1}">
<Setter Property="BorderBrush" Value="Black"></Setter>
<Setter Property="BorderThickness" Value="4"></Setter>
</Style>
</Page.Resources>
All your MyCustomGrid controls will implicitly get the style which is based on the default style.
All standard Grids will keep their default style, because they won't get the style implicitly, because you specified the x:key in the first style and therefore have to explicitly set the style of the grids. Does this clarify?

XAML and Silverlight: Applying style to subclass fails

I have created a subclass of TextBox
public class MyAwesomeTextBox : TextBox { ... }
and have set the color of all TextBoxes to be red
<UserControl>
<UserControl.Resources>
<Style TargetType="TextBox">
<Setter Property="Background" Value="Red" />
</Style>
</UserControl.Resources>
<TextBox ... />
<xyz:MyAwesomeTextBox ... />
</UserControl>
It works for all TextBoxes but not for MyAwesomeTextBoxes.
Please tell me what is wrong.
I bet you set the DefaultStyleKey property to typeof(MyAwesomeTextBox) and now the framework will only apply a style with the specialized stylekey.
See the documentation, I extracted the following part:
If you do not set the DefaultStyleKey, the default style for the base class is used. For example, if a control called NewButton inherits from Button, to use a new default Style, set DefaultStyleKey to the type, NewButton. If you do not set DefaultStyleKey, the Style for Button is used.
So what can you do now? You can either remove the defaultStyleKey (but that means the TextBox style will be applied everywhere in your application and you cannot have you AwesomeControlTemplate applied to it by default, so I think this is not what you should do now) or you can add a derived style to your resources:
<UserControl.Resources>
<Style TargetType="TextBox" x:Key="BaseStyle">
<Setter Property="Background" Value="Red" />
</Style>
<Style TargetType="TextBox" BasedOn="{StaticResource BaseStyle}">
<Style TargetType="MyAwesomeTextBox" BasedOn="{StaticResource BaseStyle}">
</UserControl.Resources>

provide a style for textblock in the gridview

I have a gridview and there're many gridviewitems in it, every gridviewitem contains some textblock, could I specify a style for all of the textblock at once? Not specify the style one by one?
You should be able to leverage the object resources and name it implicitly. Something like;
<GridView>
<GridView.Resources>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Red"/>
<!-- Etc, etc, etc -->
</Style>
</GridView.Resources>
</GridView>

Giving a specific style to DataGrid element (implicitly)

I'm trying implicitly apply a style for DataGrid and TextBlocks.
For TextBlock's ForeGround I need White color.
For DataGrid's rows I need Black Color.
Beside this I need White again for DataGrid's header columns.
When I globally apply an implicit style for on MainPage by
<UserControl>
<UserControl.Resorces>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="White"/>
</Style>
</UserControl.Resorces>
</UserControl>
Making TextBlock's Foreground White operation is done! But beside this all of elements
in DataGrid (By default content elements are textblock's I think) turn to White color.
It doesn't look good White on white as you guess :)
So how can I particularly specify DataGrid's elements Foreground to black?
I can do it by using same technic shown below ,but this is an expensive operation for each DataGrid. As a con more I want DataGrid's HeaderColumns white again.This operation make them all black.
Is there an explicit way such as we do in css styles?
Here is what I tried to achieve this goal by Control template. But no chance because of being DataGrid's ContentControl is dynamic.
<DataGrid>
<DataGrid.Resources>
<Style targetType="TextBlock">
<Setter Property="Foreground" Value="Black"/>
</Style>
<DataGrid.Resources>
In fact we use Telerik's RadGridView but I give a sdk's DataGrid example to make question more global.
<Style TargetType="sdk:DataGrid">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="RowDetailsTemplate" Value="{StaticResource DataTemplate1}"/>
<Setter Property="Template" Value="{StaticResource ControlTemplate1}"/>
</Style>
<ControlTemplate x:Key="ControlTemplate1" TargetType="sdk:DataGrid">
<Grid/>
</ControlTemplate>
<DataTemplate x:Key="DataTemplate1">
<Grid/>
</DataTemplate>
Thanks in advance!
If it were me I would pull out the full control templates and style them accordingly instead of trying to just do adhoc setter changes to override bits of the original template. In Expression Blend right click, choose "Edit Template -> Edit A Copy" and break out the templates for your rows etc and apply those implicitly with StaticResource instead.

Can I apply a style to a XAML element based on what parent element it has?

What is the correct way to apply styles on elements with a condition on their parent element's type, i. e. only if they are children of certain other elements?
In my case, I want to apply some exact button width and height, but only if those buttons are direct children of a stackpanel. Additionally, a second style should be applied to images within those buttons (glyphs).
How do I define a button style that only affects buttons on a stackpanel, but not those buttons placed directly on a grid?
Is it possible to add additional conditions such as only stackpanels with orientation="horizontal"?
Can I define "tree conditions" like only images on buttons on [horizontal] stackpanels?
As 90% of all buttons in my application are those on the stackpanels, so far I've applied the style to all buttons and images and overrode it where necessary. But this isn't the best solution, is it?
Preferably, the solution would deal with all the conditions in the style definition, so I won't have to explicitly assign that style to every single one of my stackpanels.
<StackPanel>
<StackPanel.Resources>
<Style x:Key="Rectangle1" TargetType="Rectangle">
<Setter Property="Stroke" Value="Black" />
<Setter Property="Fill" Value="White" />
</Style>
</StackPanel.Resources>
<UniformGrid Columns="10">
<UniformGrid.Resources>
<Style TargetType="Rectangle" BasedOn="{StaticResource Rectangle1}">
<Setter Property="Fill" Value="Red" />
</Style>
</UniformGrid.Resources>
</UniformGrid>
</StackPanel>