开发CustomControl和属性

时间:2013-10-02 09:17:00

标签: xaml custom-controls

我正在寻找信息 - 视频,教程,书籍等,用于开发自定义控件,例如http://www.telerik.com/

这意味着我想开发我的自定义控件,让我们举个例子 - Expander。

这是我的扩展程序代码:

<UserControl x:Class="PhoneApp16.Expander"
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"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
d:DesignHeight="480" d:DesignWidth="480">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Rectangle Fill="Wheat"/>
    <StackPanel Grid.Row="1"/>
</Grid>

这就是它在主要表格上的表现:

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <local:Expander HorizontalAlignment="Left" VerticalAlignment="Top" Width="456"/>
</Grid>

但我想添加自己的属性,如:

IsExpanded=true/false which sets Expanders StackPanel visibility to visible or collapsed

我知道ValueConverters但是如何在我的扩展器的XAML中实现这个属性,所以它看起来像:

<local:Expander IsExpanded="false" HorizontalAlignment="Left" VerticalAlignment="Top" Width="456"/>

赞赏书籍,视频等链接 - 最重要的是从一开始(对于傻瓜);

1 个答案:

答案 0 :(得分:0)

您必须在用户控件的代码benind中添加DependencyProperties。例如,假设以下XAML是您的用户控件:

<UserControl 
    x:Class="MyProject.Data.Controls.Elements.Editors.Select"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Root">

    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="120"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Border 
            Grid.Column="0"
            CornerRadius="0,0,0,0">

            <TextBlock
                x:Name="MyBlock"
                TextTrimming="CharacterEllipsis"
                Text="{Binding ElementName=Root, Path=Label, Mode=OneWay}"
                VerticalAlignment="Center"
                HorizontalAlignment="Stretch"
                FontSize="12"
                TextAlignment="Right"
                FontFamily="Segoe UI Semibold"
                Margin="4,0,4,0"/>

        </Border>

    </Grid>

</UserControl>

这是它背后的代码:

public partial class Select : UserControl
{
    /// <summary>
    /// 
    /// </summary>
    public Select()
    {
        InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    public string Label
    {
        get { return (string)GetValue(LabelProperty); }
        set { SetValue(LabelProperty, value); }
    }

    /// <summary>
    /// Identifies the <see cref="Label"/> dependency property.
    /// </summary>
    public static readonly DependencyProperty LabelProperty =
    DependencyProperty.Register("Label", typeof(string), typeof(Select), new PropertyMetadata(string.Empty));

}

现在,您可以在表单中设置控件的Label属性。设置它时,此值将转到名为MyBlock的TextBlock的Text属性。

创建依赖项属性很重要,否则绑定将不起作用。

这是一本很好的参考书: WPF Control Development Unleashed: Building Advanced User Experiences

除了MSDN有几个视频和培训材料:

http://msdn.microsoft.com/en-us/vstudio/aa496123