在MainPage中,前台代码:


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>
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>
后台代码:


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 }
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 }
自定义一个控件,定义一个依赖属性,接受页面传过来的值。
前台代码:


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>
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>
后台代码如下:


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 }
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要嵌套的场景。