簡體   English   中英

如何在不使用WPF的情況下在Silverlight 5中使用generic.xaml創建自定義控件?

[英]How to create custom controls using generic.xaml in Silverlight 5 without using WPF?

我想使用generic.xaml創建自定義控件數據網格。我已經使用Silverlight 5中的用戶控件模板創建了自定義控件。我沒有使用WPF。 如何使用generic.xaml創建?

您所描述的在Silverlight中稱為Templated Control

您必須提供三件事:

  • 包含控件邏輯的類
  • 具有ControlTemplate的默認Style
  • generic.xaml中包含默認Style的條目

示例:MyFeeblefezer.cs

public class MyFeeblefezer : Control
{
    public MyFeeblefezer() { DefaultStyleKey = typeof(MyFeeblefezer); }
}

和MyFeeblefezer.xaml

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Style TargetType="MyFeeblefezer">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="MyFeeblefezer">
          <Grid>
            <!-- here goes your visible control UI parts -->
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

和主題/generic.xaml

<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/MyFeebleProject;Component/MyFeeblefezer.xaml"/>
  </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM