當孩子加入StackPanel
時,有效的方法是什麼?我以StackPanel
爲例,它可能是Grid
,WrapPanel
或任何派生自Panel的控件。如何知道添加到StackPanel的孩子?
我想以兩種方式添加控件。使用AttachedProperty的XAML和PropertyChanged。
下面是一些代碼:
public class Region : UserControl
{
public static readonly DependencyProperty RegionNameProperty;
static Region()
{
RegionNameProperty = DependencyProperty.RegisterAttached(
"RegionName",
typeof(string),
typeof(Region),
new UIPropertyMetadata(new PropertyChangedCallback((s,e)=>
{
if(s is StackPanel)
{
StackPanel stack = (StackPanel)s;
stack.Children.Add(new Button(){Content="Test2"});
}
}))
);
}
}
XAML:
<StackPanel Name="stack2" local:Region.RegionName="MyRegion1">
<Button Content="Test 1"/>
</StackPanel>
在這裏,您可以看到按鈕 「測試2」 之前按鈕 「測試1」 增加。
感謝,
你想要什麼,對於?? –
@HighCore我在附加屬性的Setter上添加了一些控件。我認爲附加屬性的Setter在XAML添加控件之前被觸發。我需要知道何時通過XAML添加控件並對它們重新排序。 – Dilshod
我修改了我的答案,看看。 –