如何选择在用户控件上加载内容的位置? [英] How can I chose where content is loaded on a user control?

查看:65
本文介绍了如何选择在用户控件上加载内容的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个非常基本的用户控件,我可以最好地描述它与具有一些自定义依赖项属性的边框类似。我想将用户lodes作为内容放在我的用户控件的中心。听到的是我正在尝试做的事情的模拟。



  <   usercontrol1  >  
< grid >
< border >
< 其中 i 喜欢 content to load = >
< border >
< grid >
< usercontrol1 >





所以当在XAML中使用以下将导致由边框包围的文本块。



 <  窗口 >  
< usercontrol1 >
< textblock text = 用边框环绕我 margin = 5 >
< usercontrol1 >
< window >





这只是一个模拟,不会编译。我没有IDE所在的位置。如果我明天上班前没有人回答,我会用有效的代码更新。还要记住,我的实际用户控件还有很多功能。我将其删除以简化问题所以请不要提供建议不使用用户控件的答案,除非因为我需要使用自定义控件。



我尝试了什么:



我尝试使用内容演示者,但无论我做了什么,用户加载的内容都替换了用户控件。

解决方案

使用Philippe Moris建议我可以跨越内容演示者必须应用ControlTemplate或者只是设置内容属性两次这就是为什么我的整个控件被用户提供的内容取代。以下代码对我有用,但是在构建项目之后,XAML设计器抛出一个'MyControl'ControlTemplate TargetType与模板类型'UserControl'错误不匹配。这是一个错误,从项目中删除bin和obj文件夹会删除错误,设计师可以再次使用,直到重建为止。



 <   UserControl     x:Class   =  WpfApplication20.MyControl < span class =code-attribute> 

< span class =code-attribute> xmlns = http: //schemas.microsoft.com/winfx/2006/xaml/presentation\"

xmlns: x = http://schemas.microsoft.com/winfx/2006/xaml

xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006

xmlns:d = http://schemas.microsoft.com/expression/blend/ 2008

xmlns:local = clr-namespace:WpfApplication20

mc:Ignorable = d >
< UserControl.Template >
< ControlTemplate < span class =code-attribute> TargetType = {x:输入local:MyControl} >
< 边框 背景 = 黑色 BorderBrush = 黑色 >
< ContentPresenter > < / ContentPresenter >
< / Border >
< / ControlTemplate >
< / UserControl.Template >
< / UserControl >


I'm creating a pretty basic user control that I can best describe as similar to a border with some custom dependency properties. I would like to place what ever the user lodes as content in the center of my user control. Hear is a stripped down mock up of what I'm trying to do.

<usercontrol1>
   <grid>
     <border>
        <"this is where i would like content to load="">
     <border>
   <grid>
<usercontrol1>



So when used in XAML the below would result in a text block surrounded by a border.

<window>
   <usercontrol1>
      <textblock text="Surround me with a border" margin="5">
   <usercontrol1>
<window>



This is just a mock up and will not compile. I don't have an IDE where I'm at. If no one has answered before I go to work tomorrow I'll update with valid code. Also remember that my actual user control has a lot more to it. I stripped this down to simplify the question so please don't provide answers that suggest not using a user control unless its because I'll need to use a custom control.

What I have tried:

I tried using a content presenter but no matter what I've done user loaded content replaced everything in the user control.

解决方案

Using Philippe Moris advise I can across that content presenter must be applied with a ControlTemplate or your just setting the content property twice and that was why my entire control was replaced by user supplied content. The following code worked for me, but after you build the project the XAML designer throws a 'MyControl' ControlTemplate TargetType does not match templated type 'UserControl' error. This is a bug and removing the bin and obj folders from the project removes the error and the designer can be used again until its is rebuilt.

<UserControl x:Class="WpfApplication20.MyControl"

             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 

             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

             xmlns:local="clr-namespace:WpfApplication20"

             mc:Ignorable="d">
    <UserControl.Template>
        <ControlTemplate TargetType="{x:Type local:MyControl}">
            <Border Background="Black" BorderBrush="Black">
                <ContentPresenter></ContentPresenter>
            </Border>
        </ControlTemplate>
    </UserControl.Template>
</UserControl>


这篇关于如何选择在用户控件上加载内容的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆