hey there, i have written a custom control for my App so things开发者_开发技巧 get a little bit easier for me,and so far it worked great , now i wanted to bind some data to the wrapped content but Output says that i have a binding error and my "Items" property is searched at "CLIENT.UI.SinglePageControl" instead of "CLIENT.MainPage"....
<phone:PhoneApplicationPage
x:Class="CLIENT.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:CLIENT.UI"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<Grid>
<ui:SinglePageControl HeaderTitle="Connections">
<ui:SinglePageControl.PageContent>
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
<Image Source="UI/PICS/list_connection.png"/>
<TextBlock Text="{Binding ItemText}" TextWrapping="Wrap" Foreground="Black"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ui:SinglePageControl.PageContent>
</ui:SinglePageControl>
</Grid>
Try giving your control an x:Name value and then in your binding statement include ElementName=<x:Name>
<phone:PhoneApplicationPage
x:Name="pa"
x:Class="CLIENT.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="clr-namespace:CLIENT.UI"
mc:Ignorable="d"
d:DesignWidth="480"
d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="PortraitOrLandscape"
Orientation="Portrait" shell:SystemTray.IsVisible="True">
<Grid>
<ui:SinglePageControl HeaderTitle="Connections">
<ui:SinglePageControl.PageContent>
<ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items, ElementName=pa}" Background="Blue" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Orientation="Horizontal">
<Image Source="UI/PICS/list_connection.png"/> <TextBlock Text="{Binding ItemText, ElementName=pa}" TextWrapping="Wrap" Foreground="Black"/>
</StackPanel> </DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ui:SinglePageControl.PageContent>
</ui:SinglePageControl>
</Grid>
Without seeing how you're setting data contexts, etc it's difficult to tell, but judging by the details you've given, at the level of your ListBox
your data context is the SinglePageControl.PageContent
. Ordinarily, the data context of the parent (the MainPage
) would be inherited down the visual tree, so the fact that it's not in this case implies that SinglePageControl.PageContent
is setting it's own data context. If you don't need it it, then simply remove the code (this.DataContext = this;
for example) that is setting it and the data context will then be inherited.
If you have good reason for setting a data context at the page content (which would seem perfectly reasonable), then you will need to provide a way of passing that information down, but we'll need to know a bit more about what data comes from where in order to give a good solution.
精彩评论