前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wpf使用usercontrol自定义标签

wpf使用usercontrol自定义标签

作者头像
sofu456
发布2021-12-06 14:26:01
5630
发布2021-12-06 14:26:01
举报
文章被收录于专栏:sofu456

自定义标签

想向js一样自定义一个组件,过程很复杂,并没有js那样好操作,直接上代码吧,

代码语言:javascript
复制
   <UserControl.ContentTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid Background="#1979ca" Grid.Row="0">
                    <TextBox Name="title" Text="{Binding Header,ElementName=comstom}" Background="Transparent" HorizontalAlignment="Left" 
                         Foreground="White" VerticalAlignment="Center" BorderThickness="0"/>
                    <Image Source="Resource/a.png" HorizontalAlignment="Right" Margin="6,0" Height="18" MouseLeftButtonDown="Image_MouseLeftButtonDown"/>
                </Grid>
                <ContentPresenter Content="{Binding}" Grid.Row="1" />
            </Grid>
        </DataTemplate>
    </UserControl.ContentTemplate>

自定义属性

代码语言:javascript
复制
public static DependencyProperty HeaderProperty =
    DependencyProperty.Register("Header", typeof(string), typeof(TitleWindow), new PropertyMetadata(""));
public string Header
{
     get { return (string)GetValue(HeaderProperty); }
     set{SetValue(HeaderProperty, value);}
}

讲解

使用模板contenttemplate,重写usercontrol,而不是在usercontrol里面直接加grid等标签,最后uesrcontrol的content通过

代码语言:javascript
复制
<ContentPresenter Content="{Binding}" Grid.Row="1" />

直接被转移到了一个新的节点上

或者通过loaded消息,在加载后,修改content的节点,否则同一个节点在两个父节点下会报错的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/08/07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 自定义标签
  • 自定义属性
  • 讲解
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档