C# 修改MainPage.xaml中UserControl的网格
我找到了很多关于这个主题的帖子,但无法让我的应用程序正常工作,所以我在这里问了一个新问题 我想制作一个UserControl元素,它有一个可以从外部填充的网格。 这是UserControl Xaml部分:C# 修改MainPage.xaml中UserControl的网格,c#,xaml,windows-phone-8,user-controls,C#,Xaml,Windows Phone 8,User Controls,我找到了很多关于这个主题的帖子,但无法让我的应用程序正常工作,所以我在这里问了一个新问题 我想制作一个UserControl元素,它有一个可以从外部填充的网格。 这是UserControl Xaml部分: <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="70"/> <ColumnDefinition Width="*"/>
<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>
我尝试了很多方法使这个“ButtonGrid”值可见并起作用,但仍然没有。有什么想法吗
编辑
我可以用一个简单的getter获得代码背后的网格。但是,在某个地方创建东西,然后将它们放到我想要的地方,这并不是一件简单的事情。在XAML中为元素设置名称会为该对象创建一个私有字段,然后您可以在代码隐藏中访问该字段。当您尝试使用它时,它不会成为其他内容的占位符。如果要将外部内容注入XAML,请使用
ContentPresenter
或一些ContentControl
衍生工具,并将要注入的内容设置为其content
属性。这是ContentControls
通常使用的模式,因此您可以查看按钮的默认模板,例如查看此模式
所以要使用这个:
<local:MyControl>
<local:MyControl.CustomContent>
<Grid>
<!-- elements and stuff to pass in -->
</Grid>
</local:MyControl.CustomContent>
</local:MyControl>
在MyControl(CustomContent)中会有一个dependencProperty,就像您已经尝试过的那样(但它应该是type object,除非您有其他类型的特定原因)。那么控件的XAML看起来更像:
<Grid>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="2" Content="{Binding CustomContent, RelativeSource={RelativeSource FindAncestor, AncestorType=local:MyControl}}"/>
</Grid>
...
感谢约翰·伯恩提出了正确的想法。目前为止,我得到的最佳解决方案是:
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>
还有MyControl.xaml(现在是ContentControl!):
MainPage.xaml中的按钮和内容是可见的、可单击的,并且没有任何崩溃。但是我看不到MyControl.xaml内部的按钮或PhoneAccentBrush背景=/
所以这一定是正确的方式我错过了一些东西
编辑:
如果我将MyControl设置为UserControl,.Content
将覆盖其中的所有内容。
如果我将其设置为ContentControl,它似乎可以看到内部网格,因为它几乎将所有内容都放置在正确的位置。但是我仍然看不到MyControl中声明的任何其他元素。如果我不设置.Content
的话,我会看到所有的内部内容,所有的方法都在工作,当然,我不想放在里面
Edit2:
我设法做到了,但又犯了新的错误。但我想最好是把另一条线连接起来;)
谢谢您的回答!那不是我想要的。我想将UserControl用作“标准设计”,并在normal.xaml中定义内部的“元素和内容”。我将尝试反转您的建议,并使用UserControl中的ContentPresenter。好的,我明白我误解了。。现在就尝试实施。当它起作用或者我放弃的时候,我会再发一次:现在我尝试了一点。遗憾的是,Windows Phone不支持取消存储类型。( ). 我还不确定FindAncestor
。尝试找到一种WP8方法
<Grid>
<Grid.ColumnDefinitions>
...
</Grid.ColumnDefinitions>
<ContentPresenter Grid.Column="2" Content="{Binding CustomContent, RelativeSource={RelativeSource FindAncestor, AncestorType=local:MyControl}}"/>
</Grid>
<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>
<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>