简体   繁体   English

在MainPage.xaml中修改UserControl的网格

[英]Modify Grid of a UserControl in MainPage.xaml

I found many threads about this topic but couldn't make my app working, so I ask a new question here. 我找到了许多与此主题相关的主题,但无法使我的应用正常运行,因此我在这里提出了一个新问题。

I want to make a UserControl-element that has a Grid that can be filled from outside. 我想制作一个UserControl元素,它具有一个可以从外部填充的网格。 this is the UserControl-Xaml-Part: 这是UserControl-Xaml-Part:

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="70"/>
         <ColumnDefinition Width="*"/>
         <ColumnDefinition Width="Auto"/>
         <ColumnDefinition Width="*"/>
         <ColumnDefinition Width="70"/>
     </Grid.ColumnDefinitions>

     <Grid Grid.Column="2" x:Name="ButtonGrid"/>
</Grid>

Outside in my MainPage.xaml I want to use it like this: 在MainPage.xaml的外部,我想这样使用它:

<UserControls:MyControl>
    <UserControls:MyControl.ButtonGrid>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
            </Grid.ColumnDefinitions>
            // Add elements and stuff
        </Grid>
    </UserControls:MyControl.ButtonGrid>
</UserControls:AppBar>

But I don't get this far. 但是我还不算太远。 It says that "ButtonGrid" was not recognized or couldn't access Member (my translation from german error-message). 它说“ ButtonGrid”未被识别或无法访问会员(我的翻译是德语错误消息)。 But when I type UserControls:MyControl. 但是当我键入UserControls:MyControl. it suggests ButtonGrid . 它建议ButtonGrid

The MyControl.xaml.cs looks like this: MyControl.xaml.cs看起来像这样:

namespace myApp
{
    public partial class MyControl: UserControl
    {
        public Grid ButtonGrid { get; set; }

        // or
        public Grid ButtonGrid
        {
            get { return (Grid)GetValue(ButtonGridProperty); }
            set { SetValue(ButtonGridProperty, value); }
        }
        public static readonly DependencyProperty ButtonGridProperty =
            DependencyProperty.Register("ButtonGrid", typeof(Grid), typeof(MyControl), new PropertyMetadata(new Grid()));

        public MyControl()
        {
            InitializeComponent();
        }
    }
}

I tried many ways of making this "ButtonGrid"-value visible and working but still it doesn't. 我尝试了多种方法来使“ ButtonGrid”值可见并正常工作,但事实并非如此。 Any Ideas? 有任何想法吗?

EDIT I can get the Grid in the codebehind with a simple getter. 编辑我可以使用简单的getter在代码背后获取网格。 But its just not straight-forward to create stuff somewhere and THEN put them to the place I want them to be. 但是在某处创建东西并不是那么简单,然后将它们放到我希望它们成为的地方。

Setting a Name on an element in XAML creates a private field for that object that you can then access in code-behind. 在XAML中的元素上设置名称会为该对象创建一个私有字段,然后您可以在代码隐藏中访问该私有字段。 It does not make it a placeholder for other content as you're trying to use it. 在您尝试使用它时,它不会使其成为其他内容的占位符。 If you want to inject external content into your XAML use a ContentPresenter or some ContentControl derivative and set the content you want to inject to its Content property. 如果要将外部内容注入XAML,请使用ContentPresenter或某些ContentControl派生类,并将要注入的内容设置为其Content属性。 This is the pattern used in general for ContentControls so you can look at the default template for Button for example and see this pattern. 这是ContentControls通常使用的模式,因此您可以查看例如Button的默认模板并查看此模式。

So to use this: 所以使用这个:

<local:MyControl>
  <local:MyControl.CustomContent>
    <Grid>
      <!-- elements and stuff to pass in -->
    </Grid>
  </local:MyControl.CustomContent>
</local:MyControl>

You would have a DependencyProperty in MyControl (CustomContent) like you tried already (but it should be type object unless you have a specific reason for some other type). 就像您已经尝试过的那样,您将在MyControl(CustomContent)中具有DependencyProperty(但应为类型object,除非您有其他特定类型的特殊原因)。 Then your control's XAML would look more like: 然后,您控件的XAML看起来更像:

<Grid>
  <Grid.ColumnDefinitions>
    ...
  </Grid.ColumnDefinitions>

  <ContentPresenter Grid.Column="2" Content="{Binding CustomContent, RelativeSource={RelativeSource FindAncestor, AncestorType=local:MyControl}}"/>
</Grid>

Thanks to John Bowen for the right idea. 感谢John Bowen提出了正确的想法。 Now the best solution I've got so far is: MainPage.xaml 现在,到目前为止,最好的解决方案是:MainPage.xaml

<local:MyControlx:Name="Control" Grid.Row="1" Grid.RowSpan="2"
                                Height="98" VerticalAlignment="Bottom">
    <local:MyControl.Content>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
                <ColumnDefinition Width="130"/>
            </Grid.ColumnDefinitions>

        <!-- Stuff inside that works! -->

        </Grid>
    </local:MyControl.Content>
</local:MyControl>

And MyControl.xaml (which is a ContentControl now!): 和MyControl.xaml(现在是ContentControl!):

<Grid x:Name="MyCtrl" Background="{StaticResource PhoneAccentBrush}"
      Height="98" VerticalAlignment="Bottom"
      Width="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ActualWidth}">
    <Grid.RenderTransform>
        <TranslateTransform x:Name="moveTransform" />
    </Grid.RenderTransform>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="70"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
       <ColumnDefinition Width="70"/>
    </Grid.ColumnDefinitions>

    <ContentPresenter Grid.Column="2"
                      Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=DataContext}"/>

    <!-- Buttons and stuff that should be always there in Grid.Column=0 and Grid.Column=4 -->

    </Button>
</Grid>

The buttons and stuff from MainPage.xaml are visible, clickable and nothing crashes. MainPage.xaml中的按钮和内容可见,可单击,没有崩溃。 But I don't see the buttons or PhoneAccentBrush-background from the inside of MyControl.xaml =/ So this must be the right way I just miss something.. 但是我没有从MyControl.xaml = /的内部看到按钮或PhoneAccentBrush-background,所以这肯定是我错过某些事情的正确方法。

Edit: If I make MyControl to a UserControl the .Content overwrites everything that was inside. 编辑:如果我将MyControl设为UserControl,则.Content覆盖其中的所有内容。 If I make it a ContentControl it seems to see the inner Grid because it places everything almost in the right place. 如果我将其设为ContentControl,则似乎会看到内部Grid,因为它将几乎所有内容放置在正确的位置。 But still I can't see any other elements that are declared inside the MyControl. 但是我仍然看不到MyControl内部声明的任何其他元素。 If I don't set the .Content I see all internal stuff, all methods working but of course nothing that I wanted to place inside is there. 如果我不设置.Content我会看到所有内部内容,所有方法都有效,但当然没有我想放置的内容。

Edit2: I've managed it somehow but stumbled into new errors. Edit2:我已经以某种方式管理了它,但是偶然发现了新的错误。 But I guess its better to link the other thread ;) Keep a reference to objects passed to a UserControl 但是我想链接另一个线程会更好;) 保留对传递给UserControl的对象的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM