我为我的Windows Phone 8开发了UserControl,如下所示。
<UserControl x:Class="SpinrWindowsMobile.UserControls.ProgressiveLongListSelector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
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="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<phone:LongListSelector Grid.Row="0" Name="longlistselector">
</phone:LongListSelector>
<StackPanel Grid.Row="1">
<ProgressBar Name="listProress" IsIndeterminate="True"></ProgressBar>
<TextBlock Name="ProgressText" Text="Loading..."></TextBlock>
</StackPanel>
</Grid>
</UserControl>
正如您在上面的xaml中看到的,我在Grid Control中使用了 LongListSelector和StackPanel 。 我在我的MainPage.xaml中使用此控件,如下所示。
<phone:PhoneApplicationPage
x:Class="SpinrWindowsMobile.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:UserControls="clr-namespace:SpinrWindowsMobile.UserControls"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<UserControls:ProgressiveLongListSelector>
</UserControls:ProgressiveLongListSelector>
</Grid>
</phone:PhoneApplicationPage>
到目前为止,这很好,但我想做一些如下的事情。
<UserControls:ProgressiveLongListSelector>
<UserControls:ProgressiveLongListSelector.longlistselector
ItemsSource="Binding" ItemTemplate="{staticresource myTemplate}">
</UserControls:ProgressiveLongListSelector.longlistselector>
</UserControls:ProgressiveLongListSelector>
如何访问用户控件的 longlistselector,它是 UserControl 的元素/组件?
这样做的好处是I can directly set the LongListSelector Properties in the xaml(in which i am embedding My usercontrol) itself
。对我来说,这种东西是今天的要求。
任何人都可以指导我如何做到这一点吗?
答案 0 :(得分:1)
由于您正在扩展和修改LongListSelector,我建议继承并重新设置LongListSelector而不是将其放在UserControl中。这将允许您访问LongListSelector上的所有现有属性和方法,并使用新的ProgressiveLongListSelector,就像使用LongListSelector一样。
首先,您可以创建一个子类LongListSelector的新类:
public class ProgressiveLongListSelector : LongListSelector {
public ProgressiveLongListSelector() {
DefaultStyleKey = typeof(ProgressiveLongListSelector);
}
}
请注意 DefaultStyleKey 。这就是新控件模板的来源。
现在,您可以在App.xaml资源中放置以下样式。请注意, TargetType 是 ProgressiveLongListSelector 。这就是 DefaultStyleKey 将找到您的新默认样式的方式。
<Style TargetType="phoneApp2:ProgressiveLongListSelector">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="phoneApp2:ProgressiveLongListSelector">
<Grid Background="{TemplateBinding Background}" d:DesignWidth="480" d:DesignHeight="800">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="ScrollStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.5" />
</VisualStateGroup.Transitions>
<VisualState x:Name="Scrolling">
<Storyboard>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="VerticalScrollBar" />
</Storyboard>
</VisualState>
<VisualState x:Name="NotScrolling" />
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Grid Margin="{TemplateBinding Padding}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>
<ViewportControl x:Name="ViewportControl" HorizontalContentAlignment="Stretch" VerticalAlignment="Top" />
<ScrollBar x:Name="VerticalScrollBar" Grid.Column="1" Margin="4,0,4,0" Opacity="0" Orientation="Vertical" />
</Grid>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
此样式/模板是默认LongListSelector模板的副本(从Blend中提取)。从这里,您可以将UserControl中的其他元素(例如ProgressBar和TextBlock)添加到模板中。
答案 1 :(得分:0)
您可以做的是使用DependencyProperty机制公开UserContorl的属性。然后,您可以在使用该UserControl的Page的XAML中设置它们。我不确定你是否想暴露你的VisualTree的所有部分,因为这可能在未来发生变化。但是您可以间接地暴露一些影响UserControl行为的属性。
以下是一个如何做到这一点的例子:
(这个例子取自我的代码,但我想你可以弄清楚如何调整它)
首先在UserControl中声明DependencyProperty:
public partial class MyUserControl : UserControl
{
public bool IsEditingMode
{
get { return (bool)GetValue(IsEditingModeProperty); }
set { SetValue(IsEditingModeProperty, value); }
}
public static readonly DependencyProperty IsEditingModeProperty =
DependencyProperty.Register("IsEditingMode", typeof(bool), typeof(MyUserControl), new PropertyMetadata(false, IsEditingModeChanged));
}
private static void IsEditingModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
// this will be called when someone would set the exposed property to some new value
}
接下来,将MyUserControl添加到某个页面并设置该属性:
<phone:PivotItem Header="{Binding Path=LocalizedResources.MyPivotHeader, Source={StaticResource LocalizedStrings}}" Margin="0">
<my:MyUserControl x:Name="People" IsEditingMode="True"/>
</phone:PivotItem>
注意如何在XAML中设置IsEditingMode
。当然,您也可以使用public bool IsEditingMode
周围的IsEditingModeProperty
属性包装器从代码中设置它。