gpt4 book ai didi

wpf - TextBox.IsLoaded 在可见后返回 False

转载 作者:行者123 更新时间:2023-12-04 20:38:56 26 4
gpt4 key购买 nike

DataGrid我用 CellTemplateCellEditingTemplate .在两个数据模板中 FrameworkElement.IsLoaded Property返回 False即使我能看到 TextBlock , 使用 TextBoxFocus()电话已返回 True .

这是一个错误吗?或者有人可以解释一下,这种行为的原因是什么?

我创建了这个示例应用程序用于演示目的。

MainWindow.xaml.cs

namespace WpfApplication
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

this.DataContext = new List<string> { "Row1", "Row2" };
}
}

public class FocusAttached
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}

public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}

public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached("IsFocused", typeof(bool), typeof(MainWindow), new UIPropertyMetadata(false, IsFocusedChanged));

static void IsFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = obj as FrameworkElement;

if ((bool)e.NewValue)
{
Console.Write(element);
Console.Write(" IsLoaded=" + element.IsLoaded);
Console.Write(" IsVisible=" + element.IsVisible);
Console.Write(" Focusable=" + element.Focusable);
// here I call Focus()
Console.Write(" Focus() returns:" + element.Focus());
Console.WriteLine(" IsLoaded=" + element.IsLoaded);
}
}
}
}

MainWindow.xaml
<Window x:Class="WpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WpfApplication"
Title="Please click on row!" SizeToContent="WidthAndHeight">
<DataGrid ItemsSource="{Binding}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding IsLoaded, RelativeSource={RelativeSource Self}, Mode=OneWay,
StringFormat='TextBlock in CellTemplate: IsLoaded={0}'}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox c:FocusAttached.IsFocused="True"
Text="{Binding IsLoaded, RelativeSource={RelativeSource Self}, Mode=OneWay,
StringFormat='Even after call Focus(): IsLoaded={0}'}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>

<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Focusable" Value="False" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="IsEditing" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
</DataGrid>
</Window>

最佳答案

首先,您的绑定(bind)是无用的,因为 IsLoaded 不是依赖属性。没有通知,文本没有变化。

IsLoaded 是假的,因为它像测量和排列一样被延迟。该元素是可聚焦的、可见的和启用的,因此它可以被聚焦。但不能保证此时元素已经测量和渲染。这些操作在 Dispatcher 中排队。当它们被处理时, IsLoaded 将为真。试试这个:

static void IsFocusedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
FrameworkElement element = obj as FrameworkElement;

if ((bool)e.NewValue)
{
Console.Write(element);
Console.Write(" IsLoaded=" + element.IsLoaded);
Console.Write(" IsVisible=" + element.IsVisible);
Console.Write(" Focusable=" + element.Focusable);
// here I call Focus()
Console.Write(" Focus() returns:" + element.Focus());
element.Dispatcher.BeginInvoke((Action)(() =>
{
Console.WriteLine(" IsLoaded=" + element.IsLoaded);
}),
System.Windows.Threading.DispatcherPriority.Loaded);
}
}

关于wpf - TextBox.IsLoaded 在可见后返回 False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10167204/

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