自定义控件动态属性追加

在MainPage中,前台代码:

ExpandedBlockStart.gif View Code
 1 <phone:PhoneApplicationPage 
 2     x:Class= " 用户自定义控件的动态属性追加.MainPage "
 3     xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
 4     xmlns:x= " http://schemas.microsoft.com/winfx/2006/xaml "
 5     xmlns:phone= " clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone "
 6     xmlns:shell= " clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone "
 7     xmlns:d= " http://schemas.microsoft.com/expression/blend/2008 "
 8     xmlns:mc= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
 9     xmlns:local= " clr-namespace:用户自定义控件的动态属性追加 "
10     mc:Ignorable= " d " d:DesignWidth= " 480 " d:DesignHeight= " 768 "
11     FontFamily= " {StaticResource PhoneFontFamilyNormal} "
12     FontSize= " {StaticResource PhoneFontSizeNormal} "
13     Foreground= " {StaticResource PhoneForegroundBrush} "
14     SupportedOrientations= " Portrait " Orientation= " Portrait "
15     shell:SystemTray.IsVisible= " True ">
16 
17     <Grid x:Name= " LayoutRoot " Background= " Transparent ">
18         <Grid.RowDefinitions>
19             <RowDefinition Height= " Auto "/>
20             <RowDefinition Height= " * "/>
21         </Grid.RowDefinitions>
22 
23         <StackPanel x:Name= " TitlePanel " Grid.Row= " 0 " Margin= " 12,17,0,28 ">
24             <TextBlock x:Name= " ApplicationTitle " Text= " 我的应用程序 " Style= " {StaticResource PhoneTextNormalStyle} "/>
25             <TextBlock x:Name= " PageTitle " Text= " 页面名称 " Margin= " 9,-7,0,0 " Style= " {StaticResource PhoneTextTitle1Style} "/>
26         </StackPanel>
27         
28         <Grid x:Name= " ContentPanel " Grid.Row= " 1 " Margin= " 12,0,12,0 ">
29             <StackPanel>
30                 <ListBox x:Name= " lbT ">
31                     <ListBox.ItemTemplate>
32                         <DataTemplate>
33                             <local:TiTi strDN= " {Binding} " x:Name= " tt "></local:TiTi>
34                         </DataTemplate>
35                     </ListBox.ItemTemplate>
36                 </ListBox>
37 
38             </StackPanel>
39         </Grid>
40     </Grid>
41 </phone:PhoneApplicationPage>

 

后台代码:

ExpandedBlockStart.gif View Code
 1  using System.Collections.Generic;
 2  using Microsoft.Phone.Controls;
 3 
 4  namespace 用户自定义控件的动态属性追加
 5 {
 6      public  partial  class MainPage : PhoneApplicationPage
 7     {
 8          public List< string> NND =  new List< string> {  " 我是NND动态绑定成功 "" 可以吧 ", " hoho " };
 9 
10          public MainPage()
11         {
12             InitializeComponent();
13              this.lbT.ItemsSource = NND;
14         }
15     }
16 }

 

自定义一个控件,定义一个依赖属性,接受页面传过来的值。

前台代码:

ExpandedBlockStart.gif View Code
1 <UserControl x:Class= " 用户自定义控件的动态属性追加.TiTi "
2 xmlns= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
3 xmlns:x= " http://schemas.microsoft.com/winfx/2006/xaml "
4 xmlns:d= " http://schemas.microsoft.com/expression/blend/2008 "
5 xmlns:mc= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
6 mc:Ignorable= " d "
7 FontFamily= " {StaticResource PhoneFontFamilyNormal} "
8 FontSize= " {StaticResource PhoneFontSizeNormal} "
9 Foreground= " {StaticResource PhoneForegroundBrush} "
10 d:DesignHeight= " 480 " d:DesignWidth= " 480 ">
11
12 <Grid x:Name= " LayoutRoot ">
13 <StackPanel x:Name= " sp1 ">
14 <TextBlock x:Name= " tbk1 "></TextBlock>
15 </StackPanel>
16 </Grid>
17 </UserControl>

后台代码如下:

ExpandedBlockStart.gif View Code
1 using System;
2 using System.Windows;
3 using System.Windows.Controls;
4 using System.Windows.Media.Imaging;
5
6 namespace 用户自定义控件的动态属性追加
7 {
8 public partial class TiTi : UserControl
9 {
10 public string strDN
11 {
12 get { return ( string)GetValue(strDNProperty); }
13 set { SetValue(strDNProperty, value); }
14 }
15 public static readonly DependencyProperty strDNProperty =
16 DependencyProperty.Register( " strDN ", typeof( string), typeof(TiTi), null);
17
18 public TiTi()
19 {
20 InitializeComponent();
21 Loaded += new RoutedEventHandler(TiTi_Loaded);
22 }
23
24 void TiTi_Loaded( object sender, RoutedEventArgs e)
25 {
26 this.tbk1.Text = strDN;
27 }
28 }
29 }

在windowsphone中要支持banging,属性必须是依赖。

这样我在自定义控件的strDN属性中可以接受主页传过来的字符串,然后可以在后台解析字符串。来达到我的目的。

这种情况适合list要嵌套的场景。

转载于:https://www.cnblogs.com/Ewan1997/articles/2640664.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值