数据模板列需要 2 个选项卡才能访问内容
本文关键字:访问 选项 数据 | 更新日期: 2023-09-27 18:31:29
我找到了很多关于这个问题的文章,但似乎都没有用。我有 3 个数据模板列,两个带有文本框,一个带有切换按钮。首先转到单元格,然后转到内容。我尝试了许多来自其他网站的建议和我自己的发明,但没有任何运气。我可以让它在第一行工作,当我添加另一行时,但一次添加 2 个,它就会停止工作。我讨厌。数据模板。列。这是 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>
奇怪的是,这仅适用于文本框,而不适用于第三列中的切换按钮。唉!!!!
编辑:这是按钮的数据模板。它严格设置为样式和触发器目的的自定义控件。它是一个按钮,带有包含边框和内容演示器的控件模板。
<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>
在我开枪之前,请帮助我。谢谢
对我来说,
这有效:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="IsTabStop" Value="False" />
<Setter Property="Focusable" Value="False" />
</Style>
</DataGrid.CellStyle>