开发CustomControl和属性(Developing CustomControl and properties)

编程入门 行业动态 更新时间:2024-10-09 03:32:09
开发CustomControl和属性(Developing CustomControl and properties)

我正在寻找信息 - 视频,教程,书籍等,用于开发自定义控件,如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"/>

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

i'm seeking for information - videos, tutorials, books etc. for developing custom controls like http://www.telerik.com/

It means i want to develop my custom control, lets take for example - Expander.

This is my expander code:

<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>

This is how it looks on main form:

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

But i want to add my own properties like:

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

I know about ValueConverters but how to achieve this property in XAML of my expander so it looked like:

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

Links to books, videos etc. are appreciated - best of all from the beginning ( for dummies );

最满意答案

您必须在用户控件的代码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:构建高级用户体验

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

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

You have to add DependencyProperties in code benind of your user control. For example imagine that following XAML is your user control:

<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>

Here is its code behind:

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)); }

Now you can set Label property of the control in your form. When you set it this value goes to the Text property of the TextBlock named MyBlock.

It is important to create dependency properties otherwise bindings will not work.

Here is good reference book: WPF Control Development Unleashed: Building Advanced User Experiences

Besides MSDN has several video and training materials:

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

更多推荐

Expander,http,电脑培训,计算机培训,IT培训"/> <meta name="description&quo

本文发布于:2023-08-07 11:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   CustomControl   properties   Developing

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!