2009-02-18 13 views
1

我使用Expression Blend中创建一个自定义的控制:如何在Silverlight中进行自定义控件?

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="MyProject.EditableField" 
    d:DesignWidth="286" d:DesignHeight="20"> 

    <Grid x:Name="LayoutRoot"> 
     <TextBlock HorizontalAlignment="Left" Width="95" Text="Label:" TextWrapping="Wrap" x:Name="Label" FontSize="12"/> 
     <TextBox Margin="99,0,0,0" x:Name="Value" Text="Value" TextWrapping="Wrap"/> 
    </Grid> 
</UserControl> 

它包含一个TextBlock标签和一个文本框的值。我想是的,这些多添加到我的XAML用自己的标签和值,或许是这样的:

<MyProject:EditableField Margin="30,180,0,0" Height="20" HorizontalAlignment="Left" VerticalAlignment="Top" Width="286" x:Name="FaxNumber"> 
      <Label> 
       <Text>Fax number:</Text> 
      </Label> 
      <Value> 
       <Contents> 
        111-222-333 
       </Contents> 
      </Value> 
     </MyProject:EditableField> 

当我说也许,这显然是行不通的。什么是正确的语法来添加它们?

回答

2

最简单的方法是将两个新属性添加到后面的代码中,例如LabelText和ContentText。连线这些以分别设置TextBlock控件和TextBox控件的Text属性。

那么你可以使用:

<MyProject:EditableField LabelText="Fax number:" ContentText="111-222-333" ... 

正确的方法是做同样的,实现两个依赖属性(并因此支持动画,数据绑定等),并结合子控件的控件文本到这些属性。

+0

多谢,克里斯:-) – 2009-02-19 12:50:06

0

在后面的代码:

public string LabelText 
{ 
    get { return Label.Text; } 
    set { Label.Text = value; } 
} 

public string ContentText 
{ 
    get { return Value.Text; } 
    set { Value.Text = value; } 
} 
相关问题