它的工作很奇怪。我有一些從框架元素派生的類。這是愚蠢的代碼,因爲我想使它更容易:PWPF:從frameworkelement派生的刷新類
public class Strange : FrameworkElement
{
public DrawingVisual Visual;
public Strange()
{
Visual = CreateDrawingVisualRectangle();
}
protected override Visual GetVisualChild(int index)
{
return Visual;
}
protected override int VisualChildrenCount
{
get
{
return 1;
}
}
// Create a DrawingVisual that contains a rectangle.
public DrawingVisual CreateDrawingVisualRectangle()
{
var drawingVisual = new DrawingVisual();
DrawingContext drawingContext = drawingVisual.RenderOpen();
var rect = new Rect(new Point(0, 0), new Size(100, 100));
drawingContext.DrawRectangle(Brushes.Red, null, rect);
drawingContext.Close();
return drawingVisual;
}
}
接下來,我把這個類的網格。
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Click="Button_Click">Change Color</Button>
<Test:Strange Grid.Column="1" x:Name="strange"/>
</Grid>
private void Button_Click(object sender, RoutedEventArgs e)
{
strange.Visual = strange.CreateDrawingVisualRectangle();
var size = new Size(strange.ActualWidth, strange.ActualHeight);
strange.Measure(size);
strange.Arrange(new Rect(size));
strange.UpdateLayout();
}
當我按下按鈕時,奇怪對象轉到第一列(它在網格開始時呈現)。我在我的項目中有更多的邏輯,我需要刷新奇怪的對象,但它應該仍然在第二列(在它所在的位置)。
我做錯了什麼?