In a DataGrid
I use CellTemplate
and CellEditingTemplate
. In both DataTemplates FrameworkElement.IsLoaded Property returns False
even if I can see the TextBlock
, use the TextBox
and Focus()
call has returned True
.
Is this a bug? Or could someone explain, what's the reason for this behaviour?
I've created this sample application for demonstration purposes.
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>
First of all, your bindings are useless, because IsLoaded is not a dependency property. There are no notifications, there are no changes of Text.
IsLoaded is false, because it is deferred like measurement and arrangement. The element is focusable, visible, and enabled, so it can be focused. But there is no guarantee that element already measured and rendered at this point. These actions enqueued in Dispatcher. When they'll be processed, IsLoaded will be true. Try this:
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);
}
}