2014-02-22 38 views
1

我想提出一個自定義面板,其中我可以任意添加許多元素,因爲我可以,但我想從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> 
+1

請顯示您的代碼 – AymenDaoudi

+0

@AymenDaoudi我編輯了我的問題..plz看看.. – Ravi

+0

您是否想添加許多控件,例如按鈕在運行時? –

回答

0

它看起來像你想爲你的窗口創建自己的自定義模板。我們通過窗口的ControlTemplate來做到這一點。所以設置窗口的樣式吹:

<Window> 
    <Window.Style> 
     <Style TargetType="{x:Type Window}"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type Window}"> 
         <Grid> 
            <!-- Actual Window Content --> 
            <AdornerDecorator DockPanel.Dock="Bottom"> 
             <ContentPresenter /> 
            </AdornerDecorator> 
           </DockPanel> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      </Style> 
     </Window.Style> 
    <Grid> 
     /// any childs you want to add in your window's content. 
    <Grid> 

和完整的示例:

       <!-- Title bar separator--> 
           <Border Height="1" 
             DockPanel.Dock="Top" 
             Background="{DynamicResource 
        MainWindowTitleBarSeparator}" /> 

           <!-- Actual Window Content --> 
           <AdornerDecorator DockPanel.Dock="Bottom"> 
            <ContentPresenter /> 
           </AdornerDecorator> 
          </DockPanel> 
         </Border> 
        </Grid> 
        <ControlTemplate.Triggers>       
         <DataTrigger Binding="{Binding RelativeSource= 
       {RelativeSource Self}, Path=Maximized}" 
            Value="False"> 
          <Setter TargetName="MaximizeRestoreImage" 
            Property="Source" 
            Value="/MixModes.Synergy.Resources; 
       component/Resources/Maximize.png" /> 
         </DataTrigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    </Window.Style> 

<Grid> 
    /// any childs you want to add in your window's content. 
<Grid> 

</Window> 

,你也可以在一個單獨的ResourceDictionary文件中定義Stylex:Key="MyWindowCustomTemplate",那麼WindowStyle屬性設置爲{StaticResource Key=MyWindowCustomTemplate}

+0

Dis並不是我正在尋找的......實際上我想創建類似於此接口http://getbootstrap.com/javascript/#modals的東西。因此,某個按鈕單擊時,模型會顯示在我的MainWindow上。 – Ravi

+0

在我的'Modal_Main.cs'文件中,我用'Grid'作爲其子元素實例化了一個'Window'。現在我真正想要的是,當我從'Main.xaml'文件中添加一些控件時,控件應該顯示爲'Grid Panel'的子元素,而不是Window ..是否有某種方法可以實現它? – Ravi

+0

那麼你想創建一個自定義模板到你的窗口,是真的嗎? –

相關問題