I have found a lot of articles on this issue, but none of them seem to work. I have 3 DataTemplateColumns, two with textboxes and one with a toggle button. Tabbing through first goes to the cell, then the content. I have tried MANY suggestions from other sites and my own inventions without any luck. I can get it to work on the first row, and when I add another, but add 2 at a time and it stops working. I hate. DataTemplate. Columns. Here is the class im using currently, utilizing a dependency property.
public class FocusAttacher
{
public static readonly DependencyProperty FocusProperty =
DependencyProperty.RegisterAttached("Focus",
typeof(bool),
typeof(FocusAttacher),
new PropertyMetadata(false, FocusChanged));
public static bool GetFocus(DependencyObject d)
{
return (bool)d.GetValue(FocusProperty);
}
public static void SetFocus(DependencyObject d, bool value)
{
d.SetValue(FocusProperty, value);
}
public static void FocusChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
((UIElement)sender).Focus();
}
}
<DataGridTemplateColumn Header="Some Value"
MinWidth="30"
Width=".02*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Text="{Binding SomeBinding,
ValidatesOnDataErrors=True,
UpdateSourceTrigger=PropertyChanged}"
IsReadOnly="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}},
Path=DataContext.IsReadOnly}"
Style="{StaticResource SomeStyle}"
customControls:FocusAttacher.Focus="True"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
And oddly enough, this is only working for the text boxes and not the toggle button in my third column. ARGH!!!!!
Edit: Here is the DataTemplate for the Button. It is setup as a custom control strictly for style and trigger purposes. It is a Button with a controltemplate containing a border and contentpresenter.
<DataTemplate>
<customControls:MetroButton cal:Message.Attach="[Event Click] = [Action RemoveGroup($dataContext)]"
Width="15"
Height="15"
IsTabStop="False"
Focusable="False"
MouseOverBackground="LightGray">
<Button.Visibility>
<MultiBinding Converter="{StaticResource BoolsToVisibilityAndConverter}">
<Binding Path="IsReadOnly"
Converter="{StaticResource BooleanInverterConverter}"
RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGrid}}"/>
<Binding Path="IsDeletable"/>
</MultiBinding>
</Button.Visibility>
<TextBlock Text="-"
Focusable="False"
Margin="0 -6 0 0" />
</customControls:MetroButton>
</DataTemplate>
EDIT: Adding the button custom control.
<Button x:Class="Beacon.FlexCare.UI.CustomControls.MetroButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:customControls="clr-namespace:Beacon.FlexCare.UI.CustomControls"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="300"
MouseEnter="MetroButtonDefinition_MouseEnter"
MouseLeave="MetroButtonDefinition_MouseLeave"
customControls:FocusAttacher.Focus="True"
x:Name="MetroButtonDefinition">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="IsTabStop"
Value="False" />
<Setter Property="Focusable"
Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="rectangle"
BorderThickness="1.3"
Background="{TemplateBinding Background}"
BorderBrush="White"
Padding="{TemplateBinding Padding}">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Focusable"
Value="False" />
</Style>
</Border.Style>
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
OpacityMask="White">
</ContentPresenter>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed"
Value="True">
<Setter Property="Background"
Value="#005285" />
</Trigger>
<Trigger Property="IsEnabled"
Value="False">
<Setter Property="Foreground"
Value="Gray" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground"
Value="White" />
<Setter Property="Background"
Value="Transparent" />
</Style>
</Button.Style>
Please help me before I shoot myself. Thanks
For me this works:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Focusable" Value="False" />
</Style>
</DataGrid.CellStyle>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With