我在这个问题上发现了很多文章,但似乎都没有。我有3个DataTemplateColumns,两个带有文本框,另一个带有切换按钮。通过首先切换到单元格,然后是内容。我从其他网站和我自己的发明中尝试过很多建议而没有任何运气。我可以让它在第一行工作,当我添加另一行时,但一次添加2并且它停止工作。我恨。 DataTemplate中。列。这是当前使用依赖属性的类im。
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>
奇怪的是,这仅适用于文本框,而不适用于我的第三列中的切换按钮。哎呀!!!!!
编辑:这是Button的DataTemplate。它被设置为严格用于样式和触发目的的自定义控件。它是一个带有controltemplate的Button,包含一个border和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>
编辑:添加按钮自定义控件。
<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>
在拍摄之前请帮助我。感谢
答案 0 :(得分:8)
对我而言,这有效:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Focusable" Value="False" />
</Style>
</DataGrid.CellStyle>