gpt4 book ai didi

c# - DataTemplateColumn 需要 2 个选项卡才能获取内容

转载 作者:太空狗 更新时间:2023-10-29 22:53:30 26 4
gpt4 key购买 nike

我找了很多关于这个问题的文章,但是好像都没有用。我有 3 个 DataTemplateColumns,两个带有文本框,一个带有切换按钮。首先通过 Tab 键转到单元格,然后是内容。我尝试了很多来自其他网站的建议和我自己的发明,但都没有成功。我可以让它在第一行工作,当我添加另一个时,但一次添加 2 个,它就会停止工作。我恨。数据模板。列。这是我当前使用的类,利用依赖属性。

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>

关于c# - DataTemplateColumn 需要 2 个选项卡才能获取内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10216524/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com