Tôi đang cố gắng tìm hiểu các khái niệm cơ bản về việc tạo bảng tùy chỉnh trong ứng dụng WinRT XAML. Tôi đã xác định một thuộc tính phụ thuộc đính kèm và nó hoạt động như mong đợi, ngoại trừ tôi không thể tìm ra cách để gọi lại của tài sản cho một phần tử con để kích hoạt sắp xếp hoặc đo lường của vùng chứa.Làm thế nào để một container biết khi nào một đứa trẻ đã gọi là InvalidateArrange?
Cách thích hợp để trẻ có thể cho phép thùng chứa biết rằng sắp xếp và đo lường phải được gọi lại? Trong cuốn sách WPF 4 của tôi, chúng sử dụng FrameworkPropertyMetadataOptions.AffectsParentArrange nhưng nó dường như không có sẵn trong WinRT.
public class SimpleCanvas : Panel
{
#region Variables
#region Left Property
public static double GetLeft(UIElement element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
object value = element.GetValue(LeftProperty);
Type valueType = value.GetType();
return Convert.ToDouble(value);
}
public static void SetLeft(UIElement element, double value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(LeftProperty, value);
}
public static readonly DependencyProperty LeftProperty =
DependencyProperty.RegisterAttached("Left", typeof(double), typeof(SimpleCanvas),
new PropertyMetadata(0, OnLeftPropertyChanged));
public static void OnLeftPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
UIElement element = (UIElement)source;
// This doesn't cause ArrangeOverride below to be called
element.InvalidateArrange();
}
#endregion
#region Top Property
public static double GetTop(UIElement element)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
object value = element.GetValue(TopProperty);
return (value == null) ? 0 : (double)value;
}
public static void SetTop(UIElement element, double value)
{
if (element == null)
{
throw new ArgumentNullException("element");
}
element.SetValue(TopProperty, value);
}
public static readonly DependencyProperty TopProperty =
DependencyProperty.RegisterAttached("Top", typeof(double), typeof(SimpleCanvas),
new PropertyMetadata(0, OnTopPropertyChanged));
public static void OnTopPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
UIElement element = (UIElement)source;
// This doesn't cause ArrangeOverride below to be called
element.InvalidateArrange();
}
#endregion
#endregion
public SimpleCanvas()
{
}
#region Methods
protected override Size MeasureOverride(Size availableSize)
{
foreach (UIElement child in this.Children)
{
child.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
}
return new Size(0, 0);
}
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement child in this.Children)
{
double x = 0;
double y = 0;
double left = GetLeft(child);
double top = GetTop(child);
if (!double.IsNaN(left))
{
x = left;
}
if (!double.IsNaN(top))
{
y = top;
}
child.Arrange(new Rect(new Point(x, y), child.DesiredSize));
}
return finalSize;
}
#endregion
}
callback được đổi InvalidateArrange(), InvalidateMeasure(), và UpdateLayout() nhưng không breakpoint trong ArrangeOverride() cũng không MeasureOverride() được nhấn trong thùng chứa. – user263619
Có lẽ nó sẽ kiểm tra xem bất kỳ thuộc tính layout nào đã thay đổi và nếu không có thuộc tính layout nào - nó không gọi nó. Bạn có thể cần phải thay đổi một số thuộc tính bố cục, nhưng điều đó có thể phá vỡ các ràng buộc hiện có. –