简体   繁体   English

在Silverlight中创建自定义控件

[英]creating custom controls in silverlight

can you tell me what is the proper way to create custom silverlight controls? 您能告诉我创建自定义Silverlight控件的正确方法是什么吗? I want to make a control that consists of three other controls: textblock, autocompletebox and image. 我要制作一个包含其他三个控件的控件:文本块,自动完成框和图像。 The textblock will act as a label for the autocompletebox and the image will provide additional tooltip. 文本块将充当自动完成框的标签,图像将提供其他工具提示。 My question is: what type of class should I inherite from? 我的问题是:我应该继承哪种类型的课程? How can I expose properties and events of the inner controls? 如何公开内部控件的属性和事件? I found that I can set values of child controls by creating additional dependency properties in the container class and using callback function like this: 我发现可以通过在容器类中创建其他依赖项属性并使用如下回调函数来设置子控件的值:

public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
    "Text",
    typeof(string),
    typeof(MyCustomControl),
    new PropertyMetadata(
        default(string),
        (source, args) => (source as MyCustomControl).TxtValue.Text = (string)args.NewValue
    )
);

However this method works only if the property in the child control is declared as dependency property. 但是,仅当将子控件中的属性声明为依赖项属性时,此方法才有效。 But for instance in autocompletebox there is a property called ValueMemberBinding which is not a dependency property and I can't find a way how to expose this from MyCustomControl. 但是例如在autocompletebox中有一个名为ValueMemberBinding的属性,它不是依赖项属性,我找不到从MyCustomControl公开此属性的方法。

My question is: what type of class should I inherite from? 我的问题是:我应该继承哪种类型的课程?

You inherit from Control class. 您从Control类继承。

Here is a basic tutorial. 这是一个基本教程。

How can I expose properties and events of the inner controls? 如何公开内部控件的属性和事件?

As for properties - if you use xaml for setting up the controls contents and layout, you need to assign an x:Name property to each element, who's properties you want to expose. 至于属性-如果使用xaml设置控件的内容和布局,则需要为每个元素分配x:Name属性,这些元素是您要公开的属性。 Then, they bacome available in code-behind and you can expose them as public properties. 然后,它们将在后台代码中可用,您可以将其公开为公共属性。

As for events, you subscribe to the events of the inner elements and link them to your controls public events. 对于事件,您订阅内部元素的事件并将其链接到控件的公共事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM