我想提出一個自定義面板,其中我可以任意添加許多元素,因爲我可以,但我想從main.xaml動態地添加子元素,我甚至無法訪問main.xaml中的面板爲什麼如此? 我正在使用自定義模板來製作自定義面板。添加子元素的自定義面板
Library.cs
class Modal_Main : Window
{
Rectangle rect = new Rectangle();
Grid gr = new Grid();
public Modal_main()
{
this.WindowState = WindowState.Maximized;
this.AllowsTransparency = true;
this.WindowStyle = WindowStyle.None;
this.Background = Brushes.Black;
this.Opacity = 0.5;
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
this.AddChild(gr);
rect.Margin = new Thickness(350, 100, 350, 100);
rect.Fill = Brushes.White;
rect.RadiusX = 5;
rect.RadiusY = 5;
rect.Name = "rectangle";
this.MouseLeftButtonDown += Modal_main_MouseLeftButtonDown;
gr.Children.Add(rect);
this.Show();
}
private void Modal_main_MouseLeftButtonDown(object sender, RoutedEventArgs e)
{
if(this.rect.IsMouseOver == false)
this.Close();
}
我想用這個面板是這樣的Main.xaml
。所以,當我添加一個子元素中Model_Main那麼變化直接反映在我的Modal_Main類 Main.xaml
<my_namespace:Modal_Main >
<Button> My_test_button </Button>
<!--I want this button element to appear in my Modal_Main class when i add this child elements here -->
</my_namespace:Modal_Main>
請顯示您的代碼 – AymenDaoudi
@AymenDaoudi我編輯了我的問題..plz看看.. – Ravi
您是否想添加許多控件,例如按鈕在運行時? –