DataGrid2D set TextAlignment in DataGridCell - wpf

I am using the DataGrid2D posted here. I can't seem to figure out a way to align the Text inside the Cells (i.e. right, center, left). For the default WPF4 DataGrid I just set
<Style x:Key="GridTextColumnStyleLeftAligned" TargetType="TextBlock">
<Setter Property="TextAlignment" Value="Left" />
<Setter Property="Margin" Value="2" />
<Setter Property="TextWrapping" Value="WrapWithOverflow" />
<Setter Property="VerticalAlignment" Value="Center" />
</Style>
Here TextAlignment does the trick. But when using the ItemsSource2D Property to bind it to a 2-dimensional Array this does not work anymore.
Does anybody have a suggestion?

I found the problem: I am using the Alternative Style which the DataGrid2D Provides. There we already have this definiton:
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}" >
<Setter Property="TextBlock.TextAlignment" Value="Center" />
...
If I change it here it works. I will have to rewrite the class a bit, for me to set the Alignment in XAML instead of with a style.

Related

Dragablz Trapezoid Tab Background (WPF)

I'm using Dragablz and Mahapps and I want to be able to switch between modern Material Design type tabs and the trapezoid ones. I've created two TabablzControl styles I can switch between and have a CustomHeaderViewModel I can change too to adjust its appearance to fit the changing tab style. My header has a stack panel with a textblock and an icon. I can change the background colour of the SP but in the trapezoid mode I do not know how to change the trapezoid's background to match the colour theme chosen.
Here's my two styles:
<Style TargetType="{x:Type dragablz:TabablzControl}" x:Key="TabablzControlStyle">
<Setter Property="NewItemFactory" Value="{x:Static stUi:UINewItem.Factory}" />
<Setter Property="ItemsSource" Value="{Binding Items}" />
<Setter Property="ClosingItemCallback" Value="{Binding ClosingTabItemHandler}" />
<Setter Property="ShowDefaultCloseButton" Value="False" />
<Setter Property="AdjacentHeaderItemOffset" Value="-10" />
<Setter Property="ItemContainerStyle" Value="{StaticResource TrapezoidDragableTabItemStyle}" />
<Setter Property="HeaderMemberPath" Value="Header" />
<Setter Property="Background" Value="Red"/>
<Setter Property="InterTabController" Value="{StaticResource InterTabController}" />
<Setter Property="Margin" Value="0 8 0 0" />
</Style>
<Style TargetType="{x:Type dragablz:TabablzControl}" x:Key="ModernControlStyle">
<Setter Property="NewItemFactory" Value="{x:Static stUi:UINewItem.Factory}" />
<Setter Property="ItemsSource" Value="{Binding Items}" />
<Setter Property="ClosingItemCallback" Value="{Binding ClosingTabItemHandler}" />
<Setter Property="ShowDefaultCloseButton" Value="False" />
<Setter Property="AdjacentHeaderItemOffset" Value="0" />
<Setter Property="HeaderMemberPath" Value="Header" />
<Setter Property="InterTabController" Value="{StaticResource InterTabController}" />
<Setter Property="Margin" Value="0 8 0 0" />
</Style>
You can see the area around the stackpanel is lighter than the SP itself. How do I change the trapezoid colour?
Thanks, Steve
Well, I could not find an elegant way to do this so I added code to find the "Trapezoid" type in the Loaded() function of my custom header code:
var trap = TryFindParent<Trapezoid>(this);
if (null != trap)
{
trap.Background = Application.Current.Resources["AccentColorBrush1"] as SolidColorBrush;
}
TryFindParent from here : How can I find WPF controls by name or type?
It will do for me. Changing colour will typically only be done once and the setting saved.
Thanks
I think that you can create new style based on TrapezoidDragableTabItemStyle and override the Background property.
After that you must set this new style in ItemContainerStyle property of the TabablzControl.

WPF - Image source in a Style in another assembly

I have a library with some WPF Styles and another application that references this library and it styles, but I'm having a problem with an Image style. I set the Source property in the style with an image in the same library, but when I run my application it returns a XAML Markup error saying it didn't found the image.
Library/Styles/MyStyles.xaml (the .png is in Library/Resources/)
<Style TargetType="Image" x:Key="MyToolTipImage">
<Style TargetType="Image" x:Key="SmartToolTipImage">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Height" Value="12" />
<Setter Property="Width" Value="12" />
<Setter Property="Margin" Value="0, 0, 10, 0" />
<Setter Property="Source" Value="pack://application:,,,/Library;component/Resources/question_icon.png" />
</Style>
MyProject/View/ConfigView.xaml
<Image
Grid.Row="0" Grid.Column="1"
Style="{library:MyStyleRef ResourceKey=MyToolTipImage}"
ToolTip="{DynamicResource foldersReloadToolTip}"/>
The error returned is Could not find the resource question_icon.png. Also, all others styles that I have defined in this .xaml works well, like buttons and datagrids.
I resolved this by referencing this way
/Library;Component/Resources/question_icon.png
With the first / and the Component with a capital C. Also, the image must be have the Build Action set as Resource.

How to remove global FocusVisualStyle to all the Controls?

I met the same problem as Deactivate FocusVisualStyle globally.
But none of the answers can work.
So, I just want to set all the Controls in my application FocusVisualStyle="{x:Null}", any
effecive way to achieve this?
I don't want to set each control separately.
How about just putting this into your Application.Resources?:
<Style TargetType="{x:Type Control}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
To also affect non-controls as well, try this instead:
<Style TargetType="{x:Type FrameworkElement}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
</Style>
As Control is derived from FrameworkElement, they will all use this Style also.

How to make WPF DataGrid Column Header transparent

I am trying to make the column header of my WPF Datagrid to be transparent.
I am able to set it to a color without problem, but I can't have it transparent. Here is what I tried:
<Style x:Key="DatagridColumnHeaderStyle" TargetType="{x:Type tk:DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="Foreground" Value="#C2C4C6" />
</Style>
<Style x:Key="DashboardGridStyle" TargetType="{x:Type tk:DataGrid}">
<Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderStyle}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="RowBackground" Value="Transparent" />
</Style>
<tk:DataGrid Style="{StaticResource DashboardGridStyle}" >
...
</tk:DataGrid>
With this code, it seems to take the default brush.
What am I missing?
I used Snoop to take a look at what was happening. It seems that another DataGridColumnHeader is always created behind the one you can modify, and it's not affected by changes on styles. When you set a transparent background, in fact is being correctly applied, so what you see is that ghost header behind (which has the usual grey background).
If you apply a coloured background and play with Opacity, you will see how the two colours are mixed. I don't know if this can be solved.
With the answer from Natxo (thanks!), I was able to find a solution. And it is a simple one too!
Knowing that there was another DataGridColumnHeader behind the one we can modify through the ColumnHeaderStyle, I just had to set a style that will affect all DataGridColumnHeader:
<Style TargetType="{x:Type tk:DataGridColumnHeader}">
<Setter Property="Background" Value="Transparent" />
</Style>

Can you define multiple TargetTypes for one XAML style?

In HTML/CSS you can define a style which can be applied to many types of elements, e.g.:
.highlight {
color:red;
}
can be applied to both P and DIV, e.g.:
<p class="highlight">this will be highlighted</p>
<div class="highlight">this will also be highlighted</div>
but in XAML you seem to have to define the TargetType for styles, otherwise you get an error:
<Style x:Key="formRowLabel" TargetType="TextBlock">
is there a way to allow a XAML style to be applied to multiple elements or even to leave it open as in CSS?
The setters in WPF styles are checked during compile time; CSS styles are applied dynamically.
You have to specify a type so that WPF can resolve the properties in the setters to the dependency properties of that type.
You can set the target type to base classes that contain the properties you want and then apply that style to derived classes. For example, you could create a style for Control objects and then apply it to multiple types of controls (Button, TextBox, CheckBox, etc)
<Style x:Key="Highlight" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="Red"/>
</Style>
...
<Button Style="{StaticResource Highlight}" Content="Test"/>
<TextBox Style="{StaticResource Highlight}" Text="Test"/>
<CheckBox Style="{StaticResource Highlight}" Content="Test"/>
<!-- Header text style -->
<Style x:Key="headerTextStyle">
<Setter Property="Label.VerticalAlignment" Value="Center"></Setter>
<Setter Property="Label.FontFamily" Value="Trebuchet MS"></Setter>
<Setter Property="Label.FontWeight" Value="Bold"></Setter>
<Setter Property="Label.FontSize" Value="18"></Setter>
<Setter Property="Label.Foreground" Value="#0066cc"></Setter>
</Style>
<!-- Label style -->
<Style x:Key="labelStyle" TargetType="{x:Type Label}">
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="0,0,0,5" />
</Style>
I think both of these methods of declaring a style might answer your question.
In the first one, there is no TargetType specified, but the property names are prefixed with 'Label'. In the second one, the style is created for Label objects.
Another method to do it is:
<UserControl.Resources>
<Style x:Key="commonStyle" TargetType="Control">
<Setter Property="FontSize" Value="24"/>
</Style>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ListBox"/>
<Style BasedOn="{StaticResource commonStyle}" TargetType="ComboBox"/>
</UserControl.Resources>
I wanted to apply a style to a Textblock and a TextBox but the selected answer didn't work for me because Textblock doesn't inherit from Control, in my case I wanted to affect the Visibility property, so I used FrameworkElement
<Style x:Key="ShowIfRequiredStyle" TargetType="{x:Type FrameworkElement}">
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowIfRequiredStyle, UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="Visibility" Value="Visible"/>
</DataTrigger>
</Style.Triggers>
</Style>
<TextBlock Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
<TextBox Style="{StaticResource ResourceKey=ShowIfRequiredStyle}"/>
This works for the Visibility property because both items inherit from Frameworkelement and the property is defined there. Of course this will not work for properties defined only in Control, you can search the hierarchy tree and try to find a base class, anyway I thought this could help someone since this is a top search result and the selected answer is a little incomplete.
There is an alternative answer to the question. You CAN leave the TargetType parameter off the style altogether which will allow it to apply to various different controls, but only if you prefix the property name with "Control."
<Style x:Key="Highlight">
<Setter Property="Control.Foreground" Value="Red"/>
</Style>
Obviously, this only works for properties of the base control class. If you tried to set ItemsSource say, it would fail because there is no Control.ItemsSource
I got this working
<Style x:Key="HeaderStyleThin" TargetType="{x:Type Border}">
<Setter Property="Background" Value="Black" />
<Style.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background=" Value="Red" />
</Style>
</Style.Resources>
</Style>

Resources