何时使用表达式混合进行用户控件

时间:2011-05-23 16:09:26

标签: wpf xaml expression-blend

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

我有一个蛀虫,我想用它作为许多东西的背景,但它实际上是一个边界的边界 - 然后我们将放弃任何控制。我想重用这个,当然,最好的方法是什么。我想我想把它变成一个用户控件?

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

提前致谢。

2 个答案:

答案 0 :(得分:3)

这是一个允许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>

看起来像:

Sreenshot

只需随意修改边框。


编辑:为了实际回答这个问题并稍微重申我在对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相同的结果。

答案 1 :(得分:-1)

你可能最好不要制作UserControl,而是制作一个源自ContentControl或类似内容的自定义控件 - 我说这是因为你计划让它包含其他控件使用自定义控件比UserControl更容易。