The last to columns of my Data grid are read only and are supposed to ignore the tab completely since I set the Focusable property to False. The Data Grid is not custom it is only styled.
I canĀ“t get the Tab to ignore the last two columns. I would like to jump from the 8th column right to the beginning of the next row. Instead, I have to tab through the last two columns before I get to the next row.
<Style x:Key="DataGridCellFocusVisualStyle">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<Rectangle StrokeThickness="2"
Stroke="{StaticResource DarkGrayBrush}"
SnapsToDevicePixels="true"
Margin="-5 0 0 0"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}" />
<Setter Property="BorderThickness" Value="0 0 1 0" />
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<ContentControl Margin="5 0 0 0" Content="{TemplateBinding Content}"
FocusVisualStyle="{StaticResource DataGridCellFocusVisualStyle}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True" >
<Setter Property="Foreground" Value="{StaticResource BlackBrush}"/>
<Setter Property="Background" Value="{StaticResource WhiteBrush}"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="IsTabStop" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
This is the Column I want to jump over.
The Cell style is based on the CellStyle that we see in DataGridCellStyle in the XAML on top.
<Style x:Key="CalculationNumericColumnCellStyle"
BasedOn="{StaticResource LeschacoDataGridCellStyle}"
TargetType="{x:Type DataGridCell}">
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
Try the following style, it will skip all the columns where you have placed IsReadOnly = "True".
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsReadOnly" Value="true">
<Setter Property="IsTabStop" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
I answered my own question yet again. I saw that when I apply a Template to this part of the Data Grid, the Data Grid Cell seems to be seen by the TabManager as two controls in the visual tree. So the Focus Visual Style of the Data Grid Cell was the Dotted Rectangle and the Focus Visual Style of the Template was the Continuous Rectangle. So, lesson learned here is that you should not apply Templates to controls unless you absolutely have to. Heres the new XAML implementation of the Data Grid Cell if anyone is interested.
<Style x:Key="DataGridCellStyle" TargetType="{x:Type DataGridCell}">
<Setter Property="BorderBrush" Value="{StaticResource DarkGrayBrush}" />
<Setter Property="BorderThickness" Value="0 0 1 0" />
<Setter Property="FocusVisualStyle" Value="{StaticResource DataGridCellFocusVisualStyle}"/>
<!--<Setter Property="VerticalAlignment" Value="Center"/>-->
<!--<Setter Property="Padding" Value="-20 0 0 0"/>-->
<Setter Property="Margin" Value="5 0 0 0"/>
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True" >
<Setter Property="Background" Value="{StaticResource LightBlueBrush}"/>
<Setter Property="Foreground" Value="{StaticResource BlackBrush}"/>
</Trigger>
<Trigger Property="IsReadOnly" Value="True">
<Setter Property="IsTabStop" Value="False"/>
</Trigger>
</Style.Triggers>
</Style>
Related
I'm trying to add a tooltip to textbox to show it when the text input is empty in TextChangedEvent,
I have tried this solution from this post How add and show tooltip textbox WPF if textbox not empty
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Style.Triggers>
<Trigger Property="ToolTip" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
But I got this error :
Error A 'Binding' cannot be set on the 'Value' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
How can I fix this problem ?
Update:
In addition, I would like to implement something like that ( without MVVM pattern ):
Source: Facebook Website
I didn't quite understand what kind of logic you want to implement.
From my guess, this is to show the ToolTip only when the line is empty or, on the contrary, do not show it for empty.
Here are both fully working options.
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Setter Property="ToolTipService.IsEnabled" Value="False"/>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="Hello World!"/>
</ToolTip>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Setter Property="ToolTipService.IsEnabled" Value="True"/>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="Hello World!"/>
</ToolTip>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
Using:
<TextBox Style="{DynamicResource TextBoxStyle}"/>
I have breaking my brain over this for a couple of hours now. I'm only trying to center my datagrid content vertical and horizontally. Every time something seems to work it gives other problems. My code currently is like the following:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="FrameworkElement.HorizontalAlignment" Value="Center"/>
<Setter Property="FrameworkElement.VerticalAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid>
<ContentPresenter HorizontalAlignment="Center" />
<ContentPresenter VerticalAlignment="Center" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFA1A1A1"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFA1A1A1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Height" Value="35"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="#FFDDDDDD"/>
<Setter Property="Foreground" Value="#FF3E3E42"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="#FFA1A1A1"/>
<Setter Property="Foreground" Value="#FFD1D1D1"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="#FFA1A1A1"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="BorderThickness" Value="0"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
With this code the text is aligned vertical and horizontally, but... There are two problems coming with this code.
1.) I can only select a row by clicking on the text. This needs to be the whole row.
2.) If I edit a column text then the vertical alignment will go from center to top. Also the textbox is only wrapped around the text.
I have tried almost every combination but it's not working. Thanks in advance!
EDIT: I got point 2 working now. If somebody knows somethings about point 1, please let me know
So I have the following style in my Window.Resources:
<Style TargetType="TabItem" x:Key="tiS">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Height="40" Width="186">
<Border Name="tiBorder" Background="Transparent">
<ContentPresenter ContentSource="Header"
VerticalAlignment="Center"
HorizontalAlignment="Center"
TextBlock.FontSize="20"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Panel.ZIndex" Value="90"/>
<Setter TargetName="tiBorder" Property="Background" Value="{DynamicResource tiB}"/>
<!--<Setter TargetName="tiBorder" Property="Margin" Value="0,-4,0,-4"/>-->
<Setter TargetName="tiBorder" Property="BorderThickness" Value="0,0,0,1"/>
<Setter TargetName="tiBorder" Property="BorderBrush" Value="{StaticResource tiLineFade}"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Panel.ZIndex" Value="80"/>
<Setter TargetName="tiBorder" Property="BorderThickness" Value="0,0,0,1"/>
<Setter TargetName="tiBorder" Property="BorderBrush" Value="{StaticResource tiLineFade}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
This style is used on 5 TabItems, each with a different color given by 'DynamicResource tiB' in the trigger.
In each TabItem I have the following resource placed (Color1 is set in the Window.Resources):
<TabItem.Resources>
<SolidColorBrush x:Key="tiB" Color="{StaticResource Color1}"/>
</TabItem.Resources>
I was wondering, is there an easier way to do this or am I doing it right? This is the first time I'm working with styles in WPF so I want to do it right. (This code is working! Looking for a better (if there is one) solution to my situation.)
Here is the full code: http://pastebin.com/igwxgp6M
I believe this will work
<TabControl ...>
<TabControl.ItemsContainerStyle>
<Style TargetType="TabItem">
//Put triggers here
</Style>
<TabControl.ItemsContainerStyle>
</TabControl>
In my current project, I have this XAML file where I define the visual style that must be applied to different types of custom widgets.
For example, the style for a 'DirectLineButton' (a custom class that inherits from WPF's Button) is as follows:
<Style x:Key="DirectLineButtonTemplate" TargetType="{x:Type View:DirectLineButton}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="MinHeight" Value="23"/>
<Setter Property="MinWidth" Value="75"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="MyBorder"
CornerRadius="2"
BorderThickness="2"
Background="Gold"
BorderBrush="Gray">
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineAvailable">
<Setter TargetName="MyBorder" Property="Background" Value="Gold"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineIdle">
<Setter TargetName="MyBorder" Property="Background" Value="Silver"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineBusy">
<Setter TargetName="MyBorder" Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineCalled">
<Setter TargetName="MyBorder" Property="Background" Value="LightBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Now, I need to replicate this idea by defining the style for a Rectangle. Unfortunately, WPF flags an error message when I try to define a ControlTemplate for a rectangle, can you suggest a workaround for this? See the code below of what I'm trying to attempt:
<Style x:Key="MyRectangleTemplate" TargetType="{x:Type Rectangle}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="MinHeight" Value="23"/>
<Setter Property="MinWidth" Value="75"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Rectangle}">
<Border x:Name="MyBorder"
CornerRadius="2"
BorderThickness="2"
Background="Gold"
BorderBrush="Gray">
<ContentPresenter Margin="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineAvailable">
<Setter TargetName="MyBorder" Property="Background" Value="Gold"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineIdle">
<Setter TargetName="MyBorder" Property="Background" Value="Silver"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineBusy">
<Setter TargetName="MyBorder" Property="Background" Value="Green"/>
</Trigger>
<Trigger Property="View:DirectLineButton.State" Value="DirectLineCalled">
<Setter TargetName="MyBorder" Property="Background" Value="LightBlue"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Thanks in advance!
Hi you will have to do it with Border only.Controls that inherits FrameworkElement only can have Template . But Rectangle , Line etc are lighter versions they inherits only UIElement not FrameworkElement.I hope this will help. Conclusion: Rectange do not have Template property
You can't define a control template for a Rectangle, since it's not a control, but a Shape. You can only define a control template for classes derived from Control.
You should
1) use a Style (not a ControlTemplate) to have rounded borders.
2) Use a Trigger inside your style.
The following Xaml should get you closer from your goal :
<Style x:Key="MyRectangleStyle" TargetType="{x:Type Rectangle}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="MinHeight" Value="23"/>
<Setter Property="MinWidth" Value="75"/>
<Setter Property="Fill" Value="Blue"/>
<Setter Property="RadiusX" Value="2" />
<Setter Property="RadiusY" Value="2" />
<Style.Triggers>
<DataTrigger Binding="{Binding DirectLineState}"
Value="{x:Static l:DLS.Available}">
<Setter Property="Fill" Value="Gold"/>
</DataTrigger>
<DataTrigger Binding="{Binding DirectLineState}"
Value="{x:Static l:DLS.Idle}" >
<Setter Property="Fill" Value="Silver"/>
</DataTrigger >
<DataTrigger Binding="{Binding DirectLineState}"
Value="{x:Static l:DLS.Available}">
<Setter Property="Fill" Value="Green"/>
</DataTrigger >
<DataTrigger Binding="{Binding DirectLineState}"
Value="{x:Static l:DLS.Called}">
<Setter Property="Fill" Value="LightBlue"/>
</DataTrigger >
</Style.Triggers>
</Style>
(notice that :
1) i changed the key for "MyRectangleStyle".
2) If you want this Style to be the default Style, do not give it a Key, but just a TargetType...
3) ...OR set the Key to "{x:Type Rectangle}". msdn seems to prefer that way.
btw : shouldn't you be using DataTriggers on public properties instead of Triggers ? but i don't know the whole architecture of your application. )
Rq : for the code above to work, you need :
1) a public proprety called DirectLineState raising NotifyPropertyChanged on change.
2) an enum called DLS defined in a separate file (like a class) in your project
3) you need "l" to be your project NameSpace.
<xmlns:l="clr-namespace:MyProjectNameSpace" >
4) Assign Style and set proper DataContext for the Rectangle
I have a minor issue. We'd like to put as much stylistic items in the styles and outside of the control templates, to make themeing easier. So for the scrollbar's repeatbutton, I can get all of this to work but IsPressed. That only works from the template.
So the template is (basically):
<ControlTemplate x:Key="ScrollBarButtonCT" TargetType="{x:Type RepeatButton}">
<Border
x:Name="borderRepeatButton"
Margin="1"
CornerRadius="2"
Background="{TemplateBinding Background}">
<Path x:Name="pathArrow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Fill="{DynamicResource ThumbBrush}"
Data="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="true">
<Setter TargetName="borderRepeatButton" Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
And the style is
<Style x:Key="ScrollBarButtonStyle" TargetType="{x:Type RepeatButton}">
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="IsTabStop" Value="false"/>
<Setter Property="Background" Value="{DynamicResource ScrollBarBGBrush}"/> <!-- borderRepeatButton -->
<Setter Property="OpacityMask" Value="{DynamicResource ThumbBrush}"/> <!-- pathArrow-->
<Setter Property="Template" Value="{StaticResource ScrollBarButtonCT}"/>
<Style.Triggers>
<!--<Trigger Property="IsPressed" Value="true"> .... this doesn't work coming from the style
<Setter Property="Background" Value="{DynamicResource ThumbPressedBrush}" />
</Trigger>-->
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{DynamicResource ScrollBarDisabledBGBrush}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="{DynamicResource ThumbHoverBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
I can't get IsPressed to work from the style. Looking in Snoop IsPressed is raised just fine when using the control. What am I doing wrong? Thanks!
No idea why it doesnt work, maybe it needs static resource? u can try this to get all styles in one place.
<Style x:Key="xxxtyle" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter Property="Fill" TargetName="rectangle" Value="#FFD5D5D5"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
ps TargetType="typeName" == TargetType="{x:Type typename}"
I know this is old, but it turns out this must be a bug in the template. We could never get it to work, and talking to some people on the inside more or less confirmed it. We just left the value in the template and worked around it by swapping templates when we needed a different RepeatButton style.