具有默认内容的 WPF 自定义网格

发布于 2024-11-09 01:30:13 字数 341 浏览 0 评论 0 原文

我想创建一个继承 WPF 网格 并具有以下功能的 自定义控件

  • 它必须有一些默认行
  • 它必须有一些子控件 这些行(主要是按钮和线条) 默认
  • 非默认内容 必须可由设计师编辑或 在插入控件的窗口的 xaml 中写入内容

我尝试继承 Grid 并在构造函数中添加内容,但是一旦设计器添加更多内容,默认内容就会丢失。我尝试了很多事情,但我没能成功。有可能吗?我怎样才能做到这一点?

I want to create a Custom Control that inherits WPF Grid with these features:

  • It must have some rows by default
  • It must have some child controls in
    those rows (mainly buttons and lines)
    by default
  • The non default content
    must be editable by designer or
    writting content in xaml of the window where the control is inserted

I tried inheriting Grid and adding content in the constructor but as soon as I add more content by designer, default content is lost. I have tried lots of things but I couldn't manage to make it. Is it even possible? How could I make this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风苍溪 2024-11-16 01:30:13

马丁,你不能这么做。
网格是一种面板,其内容作为子属性。因此,如果您在 XAML 设计器中向其中添加任何内容,它将被替换。

但是,您可以覆盖子属性,并将其添加到您的类 中,如示例所示 - 例如

'Code:
<ContentProperty("Children")> _
Public Class MyGrid
Public Overloads ReadOnly Property Children As UIElementCollection
    Get
        Return Me.ContentGrid.Children
    End Get
End Property
End Class

'Markup
<Grid x:Class="MyGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
</Grid.RowDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="2" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="1" />

<Grid Name="ContentGrid" Grid.RowSpan="3"></Grid>
</Grid>

Martin, You can't do that.
Grid is a type of panel, with content as children property. So, if you add anything to it in XAML designer it will be relpaced.

However you can override childern property, and add this to your class <ContentProperty("PropertyName")> like in the example -

Ex:

'Code:
<ContentProperty("Children")> _
Public Class MyGrid
Public Overloads ReadOnly Property Children As UIElementCollection
    Get
        Return Me.ContentGrid.Children
    End Get
End Property
End Class

'Markup
<Grid x:Class="MyGrid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
</Grid.RowDefinitions>
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="2" />
<Button Content="Button" Height="23" HorizontalAlignment="Center" VerticalAlignment="Center" Width="75" Grid.Row="1" />

<Grid Name="ContentGrid" Grid.RowSpan="3"></Grid>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文