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

C# 将子元素添加到自定义面板,c#,wpf,xaml,panel,controltemplate,C#,Wpf,Xaml,Panel,Controltemplate,我正在制作一个定制的面板,我可以在其中添加任意多个元素,但我想从main.xaml动态添加子元素,我甚至无法在main.xaml中访问该面板为什么会这样?? 我在制作自定义面板时使用自定义模板 <!-- Title bar separator--> <Border Height="1"

我正在制作一个定制的面板,我可以在其中添加任意多个元素,但我想从main.xaml动态添加子元素,我甚至无法在main.xaml中访问该面板为什么会这样?? 我在制作自定义面板时使用自定义模板

                                <!-- 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>
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();
    }
                                <!-- 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>
我想在
Main.xaml
中使用类似这样的面板。这样,当我在Model\u Main中添加子元素时,该更改将直接反映在Model\u 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>
                                <!-- 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>

我的测试按钮

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

///要添加到窗口内容中的任何孩子。

                                <!-- 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>
以及完整的样本:

                                <!-- 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
文件中使用
x:Key=“MyWindowCustomTemplate”
定义
样式,然后将
窗口的
样式
属性设置为
{StaticResource Key=MyWindowCustomTemplate}

请出示您的code@AymenDaoudi我已经编辑了我的问题..请看一看..你想在运行时添加许多控件,比如按钮吗?@AliAdl是的。。。我想添加我从
Main.xaml
添加的任何控件,以反映在我的
Modal_Main.cs
文件中。您是在运行时添加的吗?或者在Xaml文件中?Dis不是我要找的…实际上我想创建类似于此界面的东西。这样一个模型就可以在单击某个按钮时显示在我的主窗口上。在我的
Modal_Main.cs
文件中,我实例化了一个
窗口
,并将
网格
作为其子元素。现在我真正想要的是,当我从
Main.xaml
文件添加一些控件时,该控件应该显示为
网格面板的子元素,而不是窗口的子元素。有什么方法可以实现吗?然后你想为你的窗口创建一个自定义模板,对吗?是的,我想为我的窗口创建一个自定义模板
                                <!-- 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>