将子元素添加到自定义面板

时间:2014-02-22 17:51:08

标签: c# wpf xaml panel controltemplate

我正在制作一个自定义面板,我可以在其中添加任意和多个元素,但我想从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 个答案:

答案 0 :(得分:0)

您似乎想为窗口创建自己的自定义模板。我们通过Window的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>

您也可以在单独的Style文件中定义x:Key="MyWindowCustomTemplate" ResourceDictionary,然后将Window的{​​{1}}属性设置为{{1} }。