【问题标题】:When to make a user control using expression blend何时使用表达式混合制作用户控件
【发布时间】:2011-05-23 16:09:25
【问题描述】:

所以我正在开发一个新应用程序,并且我正在使用 Expression Blend(第一次)创建布局和样式等,但我有一个关于何时要创建用户控件的问题。

我有一个钻孔器,我想用它作为很多东西的背景,但它实际上是一个边框中的一个边框 - 然后我们会将任何控件放入其中。我当然想重用这个,最好的方法是什么。我在想我想把它变成一个用户控件?

问题与标准边框不同——我不能只编辑资源字典中的样式,因为就像我说的那样——它是边框中的边框。

提前致谢。

【问题讨论】:

    标签: wpf xaml expression-blend


    【解决方案1】:

    这是一个允许 UIElement 内容的 UserControl 示例:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using System.Windows.Markup;
    
    namespace TestClean.UserControls
    {
        [ContentProperty("Child")]
        public partial class CustomBorder : UserControl
        {
            public static readonly DependencyProperty ChildProperty =
                    DependencyProperty.Register("Child", typeof(UIElement), typeof(CustomBorder), new UIPropertyMetadata(null));
            public UIElement Child
            {
                get { return (UIElement)GetValue(ChildProperty); }
                set { SetValue(ChildProperty, value); }
            }
    
            public CustomBorder()
            {
                InitializeComponent();
            }
        }
    }
    
    <UserControl x:Class="TestClean.UserControls.CustomBorder"
                 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" 
                 Name="control">
        <Border BorderThickness="1" CornerRadius="10" BorderBrush="Red" Background="Pink" Padding="5">
            <Border BorderThickness="1" CornerRadius="8" BorderBrush="Blue" Background="LightBlue" Padding="5">
                <ContentPresenter Content="{Binding ElementName=control, Path=Child}" />
            </Border>
        </Border>
    </UserControl>
    

    使用示例:

    <uc:CustomBorder>
        <TextBlock Text="Lorem Ipsum"/>
    </uc:CustomBorder>
    

    看起来像:

    随意修改边框即可。


    编辑:要实际回答这个问题并在某种程度上重申我在对 Tim 的回答的评论中所说的话,UserControls 适合于此,因为它们直接封装了视觉表示并允许组合,同时不需要太多自定义逻辑(理论上它们可能非常复杂并且包含很多逻辑)。它们比 CustomControls 更轻量和更直接。

    在 WPF 控件(即普通控件,包括仅从它们继承的 CustomControls)中被认为是无外观的,它们通常提供默认模板,但它们的关键方面是它们的功能,例如,如果您想到 ComboBox,它是一个包含项目的控件,它应该有一个或没有选定元素,并且应该有一个显示项目并支持选择等的下拉菜单。控件有责任提供构成此功能的必要属性和方法。由于这有时需要某些视觉元素才能存在,因此控件可以定义parts,它表示前端的最小接口。

    看看Control Authoring overview,它更深入,可能比我能更好地解释很多事情。


    另外:最终的轻量级方法是只使用模板:

    <!-- You can use any resource dictionary, specifying it in App.xaml
         simply makes it usable all over the application. -->
    <Application.Resources>
        <ControlTemplate x:Key="CustomBorderTemplate" TargetType="{x:Type ContentControl}">
            <Border BorderThickness="1" CornerRadius="10" BorderBrush="Red" Background="Pink" Padding="5">
                <Border BorderThickness="1" CornerRadius="8" BorderBrush="Blue" Background="LightBlue" Padding="5">
                    <ContentPresenter Content="{TemplateBinding Content}" />
                </Border>
            </Border>
        </ControlTemplate>
    </Application.Resources>
    
    <ContentControl Template="{StaticResource CustomBorderTemplate}">
        <TextBlock Text="Lorem Ipsum"/>
    </ContentControl>
    

    这会产生与 UserControl 相同的结果。

    【讨论】:

    • 谢谢 - 这很容易 - 唯一的事情是我不知道如何告诉 Blend。我的意思是,我可以手动输入它并且它可以工作,即 xmlns:controls="clr-namespace:Shared.UserControls;assembly=Shared" 然后 - 。我需要弄清楚这一点,以便团队其他成员可以拖放它......
    • 也许你可以在 StackOverflow 上找到一些东西,否则这是一个新问题的材料,我从来没有真正使用过设计师,所以我通常不会关心这样的东西。
    • 因为一开始我并没有真正回答你的问题,所以顺便扩展了我的答案。
    • 这真的很酷,但在这种情况下,您的第一个示例实际上更好,因为我们最终可能会在某些时候向用户控件添加一些自定义逻辑,所以很高兴将所有这些都放在此处。
    【解决方案2】:

    您最好不要创建UserControl,而是创建一个派生自ContentControl 的自定义控件或类似的东西 - 我这样说是因为您计划让它包含其他控件,这很多使用自定义控件比 UserControls 更容易。

    【讨论】:

    • 为什么投反对票?我不介意——当我错的时候总是愿意学习。只是好奇。
    • 对不起,迟到的评论,我试图先整理一个例子。 UserControls 是针对所描述的问题找到的,您可以很好地组合它们并插入内容。 CustomControls 更多用于需要封装大量逻辑的繁重功能实现。
    • 但是您不能(至少不容易)在 XAML 中将 UserControl 用作 ContentControl,这难道不是真的吗?我的意思是有类似 &lt;my:CustomControl&gt;&lt;Button Content="Hello World" /&gt;&lt;/my:CustomControl&gt; 的东西?我的理解是,使用自定义控件更容易实现这种情况,而且这似乎是原始海报想要的情况。
    • 这是一个神话,我现在有一个例子。
    • 每天学习新东西。我猜这个神话的“更容易”的部分可能来自必须自己在 UserControl 中定义 Child 属性和 ContentProperty 属性(包括我自己在内的一些人可能无法发现),同时派生自已经定义了这些部分的东西会马上给你。感谢 H.B. 的澄清,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多