WPF DataGrid Header styling - c#

I want to create a custom styled header for WPF DataGrid, but I don't know if it is possible to do what I want, and if it is how exactly should I do.
The sketch of the DataGrid is on the linked picture. The Purple header extends beyond the edge of the DataGrid and it has a little 3D bending.
DataGrid
Can I do something like this with WPF DataGrind and if yes how do I start?
Thanks!

DataGrid allows for ColumnHeaderStyle, CellStyle and many more styles to bet set. Right click DataGrid control in designer view > View Additional Templates will show you the complete list.
You have to use the following :
<Style x:Key="DataGridColumnHeaderStyle1" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="Orange"/>
<Setter Property="FontSize" Value="20"/>
<Setter Property="Padding" Value="5"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="BorderThickness" Value="0 0 3 0"/>
</Style>
This will change the column headers. And if you want to change cells too, you can provide styles for DataGridCell too.
Use Snoop tool to peek inside a DataGrid first and peek into it to see what it looks like at runtime. This will clear many of your concepts.

It is possible, but it is a lot of work and requires very good knowledge of writing WPF templates. What you essentially want to do is replace the templates for DataGrid, and all other DataGrid related conotrols such as the DataGridRow, etc
Microsoft provide a full example of how to do this here: https://msdn.microsoft.com/en-us/library/vstudio/ff506248(v=vs.100).aspx

Related

How to apply style in WPF to all controls without overriding their original styles?

I want to create a style, which will normalize margins for all controls in a specific scenario:
<Style TargetType="FrameworkElement" x:Key="MyStyle">
<Setter Property="Margin" Value="{StaticResource DialogItemsExceptTopMargin}" />
</Style>
I have then a couple of different controls: textboxes, comboboxes, checkboxes etc., to which I want to apply this style.
However, when I do that, their look is immediately reverted to the platform style (I am applying a style from 3rd party library). How can I define my style so that the original styles are kept intact?
Mind: I know, that I can use BasedOn for specific type:
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
...
</Style>
This however would require me to create completely separate styles for all possible controls, which I may use. I tried this trick with FrameworkElement and Control, but I got an error, that there is no such StaticResource.

WPF Performance using ResourceDictionary

In my WPF application I have a lot of XAML, So I'm little confused in using ResourceDictionary in WPF, Does using it fasted my application or slow it down.
EX: Lets say we have the following style for a TextBlock that we want to apply it to a lot of TextBlock's in my application:
<Style TargetType="{x:Type TextBlock}" x:Key="MainText">
<Setter Property="FontSize" Value="12" />
<Setter Property="Height" Value="20" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="FontFamily" Value="Droid Arabic Kufi" />
<Setter Property="Foreground" Value="#FF383838"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
What is the best approach for my application performance, Declared the style in ResourceDictionary like above and using it as following:
<TextBlock Style="{StaticResource MainText}" Text="Hello"/>
Or repeat the style (FontWeight, FontFamily, Foreground ... ) in each one?
Thanks in advance
There isn't a performance difference for using it either way like that. Performance strains on ResourceDictionaries are used only when the view is Initialized. The reason for the strain is because it looks first inward and then outward for the resource until it finds the first one of that key. So if the resource is in the control then it's found faster, then it searches the parent of that control and so on to the Window (usually MainWindow), and finally the Application. (Meaning the closer to the control the resource is the faster it's found and used.. but that speed is NOT an issue. It only really becomes an issue in apps that have a LOT of shared ResourceDictionary's at the App level.)
There are very efficient and clever ways to get around this constraint but it's not an issue unless your views are rendering slowly based solely on this. Once the view is rendered it has a direct reference to the source and there are no more checks. Now if the control is created, while the view is already rendered, such as a Template in an ItemsControl, and the Templates are referencing those resources then the list may show the penalty upon updating and other things of that nature.
To the point... Don't worry about performance unless the ResourceDictionary's are at the App level AND you have a lot of them.

WPF - Turn on/off keyboard from code behind

There's a custom touchscreen keyboard in my app built according to this: https://www.codeproject.com/Articles/32568/A-Touch-Screen-Keyboard-Control-in-WPF.
I also have a ResourceDictionary containing all styles and templates. In the TextBox style, I can set the keyboard ON/OFF:
<Style TargetType="{x:Type TextBox}">
<Setter Property="FontSize" Value="14" />
<Setter Property="Padding" Value="4" />
<Setter Property="k:TouchScreenKeyboard.TouchScreenKeyboard" Value="True"/>
</Style>
I would like the user to be able to turn it on or off from the UI, but can't figure out how to reach this property from code behind. I would like to make it without naming the style, since it's pretty commonly used throughout the app.
I tried this, but (no surprise) get ArgumentNotFoundException:
Style s = Application.Current.FindResource("defTextBox") as Style;
s.RegisterName("Keyboard.TouchScreenKeyboard.TouchScreenKeyBoard",false);
Any help would be appreciated!
You shoud be able to set the TouchScreenKeyboard attached property for an individual TextBox like this:
TouchScreenKeyboard.SetTouchScreenKeyboard(textBox1, false);
Changing the defintion of the implicit Style itself after it has already been applied to all TextBox elements doesn't make much sense though. You should define the default value in XAML and then change the value for individual TextBoxes dynamically at runtime if you need to.

Edit Button Visual State without Blend

One can edit Foreground and Background etc... for a Button's default state without Blend using just the properties pane or code, but it is it possible to edit the colors for the other states without Blend?
For example, all I want is a button to turn gray on "Mouse Over". Anything I have seen on Stack Overflow or on the Intertubes uses Blend. I want to do it without it. Is it possible?
An easy way to do this is using Style Triggers.
<Style x:Key="HoverButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Gray"/>
</Trigger>
</Style.Triggers>
</Style>
The style is defined in your Resources. And then when you define your button in XAML, you assign the style, like this:
<Button x:Name="MyButton" Style="{DynamicResource HoverButtonStyle}"/>
This style will change the Background brush of your Button when the mouse is over it. There are many more advanced things you can do with triggers, but this is a very simple example. If you google WPF Style Triggers, you'll find many examples, including this one which is pretty thorough.
There are other ways to do this, for example using the VisualStateManager, as described here, however if you're just trying to change the Background on hover, a style trigger is probably the simplest way.

Dynamically changing the styles of controls

I have created a screen (Screen1.xaml) that has few text boxes and dropdowns. Since the properties of all the text boxes are same, I have created a styles file(stylesheet.xaml) with the properties like width, height, Font size etc. like this
<Style x:Key="TextBox.Base" TargetType="Control" BasedOn="{StaticResource TextBlock.Base}">
<Setter Property="Width" Value="250"/>
<Setter Property="FontSize" Value="10"/>
<Setter Property="Height" Value="15"/>
<Setter Property="FontFamily" Value="Arial"/>
</Style>
Now, I want to dynamically change the properties of the controls depending on some conditions. I want to acheive this doing something in the code-behind. Please help.
You can pick a style and change it in code-behind. Depending on the way you have included it to your project there are several ways:
// the style defined in the app.xaml (you need a key)
Style globalStyle = Application.Current.Resources["Key"] as Style;
// the style defined for a control (you need its key)
UserControl control = ...
Style controlStyle = control.Resources["Key"] as Style;
// the current style of the control
Style currentStyle = control.Style;
Change the style like this:
style.SetValue(UserControl.FontSizeProperty, (float)10);
Edit:
As I read now, to change a style will only affect all controls that use it in WPF. In Silverlight, you might change the property for all controls :(

Categories

Resources