DataGrid中,我使用CellTemplateCellEditingTemplate。在两个DataTemplates中,即使我可以看到FalseFrameworkElement.IsLoaded Property也会返回TextBlock,请使用TextBox,并且Focus()调用已返回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>

最佳答案

首先,绑定没有用,因为IsLoaded不是依赖项属性。没有通知,没有文本更改。

IsLoaded为假,因为它像测量和安排一样被延迟。该元素是可聚焦,可见和启用的,因此可以使其聚焦。但是,不能保证在这一点上已经进行了测量和渲染。这些动作在分派器中排队。当它们将被处理时,IsLoaded将为true。试试这个:

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);
    }
}

10-06 12:48