何时使用表达式混合制作用户控件 [英] When to make a user control using expression blend

查看:55
本文介绍了何时使用表达式混合制作用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am working on a new app and I am using Expression Blend (for the first time) to create the layout and styles etc. but I have a question about when I would want to create a user control.

我有一个蛀虫,想用作很多东西的背景,但是它实际上是边界中的一个边界-然后我们将所有控件放入其中.我当然想重用它,最好的方法是什么.我在想将其设置为用户控件?

I have a borer that I want to use as the background for lots of stuff, however it is really a border in a border - then we will drop whatever controls into it. I want to reuse this of course, what is the best way to do that. I am thinking I want to make it into a user control?

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

The problem is unlike a standard border - I can't just edit a style in a resource dictionary because like I said - it's a border in a border.

先谢谢了.

推荐答案

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

Here is a UserControl example that allows for UIElement content:

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>

看起来像:

只需随意更改边框即可.

Just modify the borders at will.

编辑:要真正回答问题并在某种程度上重申我在对Tim的回答的评论中所说的话,UserControl非常适合此操作,因为它们直接封装了视觉表示并允许合成而又不需要太多自定义逻辑(理论上,它们可能非常复杂,并且包含很多逻辑).它们比CustomControls更轻便,直接.

To actually answer the question and somewhat reiterate what i said in a comment to Tim's answer, UserControls are appropriate for this since they directly encapsulate the visual representation and allow for composition while not requiring much custom logic (in theory they can be very complex though and include a lot of logic). They are more lightweight and immediate than CustomControls.

在WPF控件(即普通控件,包括仅继承自它们的CustomControl)中,它们被认为是看不见的,它们通常提供默认模板,但是其关键方面是其功能,例如,您想到的是ComboBox,它是一个包含项目的控件,应该具有一个或没有选定的元素,并且应该有一个下拉列表,用于显示项目并支持选择等.控件的责任是提供构成此功能的必要属性和方法.由于这有时需要某些视觉元素才能存在,因此控件可以定义各部分代表与前端的最小接口.

In WPF controls (i.e. normal controls, including CustomControls which just inherit from them) are considered to be lookless, they normally provide a default template but their key aspect is their functionality, if you think of a ComboBox for example, it's a control which contains items, it should have one or no selected element and there should be a dropdown which displays the items and supports selection etc. It is the responsibility of the control to provide the necessary properties and methods which make up this functionality. Since this sometimes requires certain visual elements to exist controls can define parts which represent the minimal interface to the front-end.

看看控件创作概述,它是更深入,可能比我能更好地解释很多事情.

Have a look at the Control Authoring overview, it is more in-depth and probably explains a lot of things better than i can.

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

Also: The ultimate lightweight method is only using Templates:

<!-- 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相同的结果.

This yields the same result as the UserControl.

这篇关于何时使用表达式混合制作用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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