ASP.net MVC V2 - 造型模板 [英] ASP.net MVC v2 - Styling templates

查看:109
本文介绍了ASP.net MVC V2 - 造型模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道如果任何人有我们如何能样式模板...就像如果我想换一个文本框,我怎么去这样做的最大长度的任何想法。

Just wondering if anyone has any ideas on how we can style templates... Like if I want to change the MaxLength of a textbox how do I go about doing this.

我想能够做的是一样的东西我可以在WPF做样式和模板......在WPF可以通过样式传递给模板,然后选择在哪里应用这些样式...对于实例如果用户设置上的控件的宽度(也就是模板),控制code之内我可以选择的宽度适用于我想要在模板或根本没有...

What I would like to be able to do is something like what I can do in WPF with styles and templates... In WPF you can pass through styles to the templates and then choose where those styles are applied... For instances if the user sets the width on a control (which is the template), within the control code I can choose to apply that width to any element I want within template or to none at all...

因此​​,我想知道是否有人的任何类似谁知道呢?

Hence I was wondering if anyone knows of anything similar?

推荐答案

我已经张贴回答类似的问题<一href=\"http://stackoverflow.com/questions/1625327/asp-net-mvc-2-editorfor-and-html-properties/1627315#1627315\">here.

I've posted an answer to a similar question here.

如果你想通用样式,可以从基TemplateViewModel类,这将支持您所需的样式派生自定义模板的型号:

If you want generic styles, you can derive your custom templates's Models from the base TemplateViewModel class which will support your required styles:

public interface ITextSpecifier
{
  int? Size { get; }
  bool AutoGrow { get; }
}

public class TemplateViewModel<T> where T: class
{
  public IDictionary<string, string> Attributes { get; }
  public ITextSpecifier TextStyle { get; private set; }
  public IColorSpecifier ColorStyle { get; }
  public T TextStyle(int size, bool autogrow)
  {
     TextStyle = new TextSpecifier(size, autogrow);
     return this;
  }
}

public class TextBoxViewModel: TemplateViewModel<TextBoxViewModel>
{
}

<%= Html.EditorFor(x => new TextBoxViewModel(Model.StringData).TextStyle(10, false)) %>

在模板:

<!-- template page derived from typed control for TextBoxViewModel -->
<input type='text' <%= Model.TextStyle.Size != null 
    ? "size='" + Model.TextStyle.Size + "'" : "" %> ... />

这是一些工作,这就是为什么我希望他们会创造在MVC v2发行了一些常用的方法。

It's a bit of work, that's why I hope they'll invent some common method in MVC v2 release.

这篇关于ASP.net MVC V2 - 造型模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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