Windows store apps 在usercontrol中使用Gridview控件

Windows store apps 在usercontrol中使用Gridview控件,windows-store-apps,Windows Store Apps,我正在尝试创建具有以下要求的UserControl Usercontrol包含一个gridview,在侧面gridview中,我需要添加一个button元素。因此,其思想是:-如果通过向ItemSource提供集合在任何其他页面中使用usercontrol,则应生成按钮列表,并且按钮内容值应为ItemSource集合中存在的类型的属性值之一 我是windows应用商店应用程序编程的新手。我试图通过创建dependency属性来公开gridview ItemSources属性,以便可以映射任何类型

我正在尝试创建具有以下要求的UserControl

Usercontrol包含一个gridview,在侧面gridview中,我需要添加一个button元素。因此,其思想是:-如果通过向ItemSource提供集合在任何其他页面中使用usercontrol,则应生成按钮列表,并且按钮内容值应为ItemSource集合中存在的类型的属性值之一

我是windows应用商店应用程序编程的新手。我试图通过创建dependency属性来公开gridview ItemSources属性,以便可以映射任何类型的ObservableCollection,并尝试公开一个dependency属性以绑定到button内容属性。但不能达到同样的效果。如果您能举出一个同样的示例应用程序,我将不胜感激

提前非常感谢。

这是一个小样本(我希望这就是你想要做的)

编辑:我最终编辑了答案,以提供采用属性路径的属性。如果有人对此问题有其他解决方案,请告诉我

为绑定路径添加依赖项属性:

首先,我们创建一个虚拟模型,为按钮提供标题:

public class SampleModel
{
    public string Title { get; set; }
}
然后,用户控件。这里最重要的是ItemsSource绑定(ElementName=UserControl)。否则,您将绑定到父DataContext中的UserControlItemsSource

编辑:自上次回答后,按钮已更改

<UserControl
x:Class="StackOverflow.ListUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:StackOverflow"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400"
x:Name="SampleUserControl">

<Grid>
    <ListView ItemsSource="{Binding UserControlItemsSource, ElementName=SampleUserControl}" Background="DeepSkyBlue" SelectionMode="None">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Button Loaded="FrameworkElement_OnLoaded"/>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>
</UserControl>

。。。以及usercontrol的代码,包括UsercontrolItemsSource:

EDIT:自上次回答后,我添加了依赖属性BindingPropertyPathFrameworkElement\u onload方法

public sealed partial class ListUserControl : UserControl
{
    public ObservableCollection<SampleModel> UserControlItemsSource
    {
        get { return (ObservableCollection<SampleModel>)GetValue(UserControlItemsSourceProperty); }
        set { SetValue(UserControlItemsSourceProperty, value); }
    }

    // Using a DependencyProperty as the backing store for UserControlItemsSource.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty UserControlItemsSourceProperty =
        DependencyProperty.Register("UserControlItemsSource", typeof(ObservableCollection<SampleModel>), typeof(ListUserControl), new PropertyMetadata(null));


    public string BindingPropertyPath
    {
        get { return (string)GetValue(BindingPropertyPathProperty); }
        set { SetValue(BindingPropertyPathProperty, value); }
    }

    // Using a DependencyProperty as the backing store for BindingPropertyPath.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty BindingPropertyPathProperty =
        DependencyProperty.Register("BindingPropertyPath", typeof(string), typeof(ListUserControl), new PropertyMetadata(string.Empty));


    public ListUserControl()
    {
        this.InitializeComponent();
    }

    private void FrameworkElement_OnLoaded(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        Binding contentBinding = new Binding();
        contentBinding.Path = new PropertyPath(this.BindingPropertyPath);
        button.SetBinding(Button.ContentProperty, contentBinding);
    }
}
公共密封部分类ListUserControl:UserControl
{
公共ObservableCollection用户控制项资源
{
get{return(ObservableCollection)GetValue(UserControlItemsSourceProperty);}
set{SetValue(UserControlItemsSourceProperty,value);}
}
//使用DependencyProperty作为UserControlItemsSource的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读从属属性UserControlItemsSourceProperty=
Register(“UserControlItemsSource”、typeof(ObservableCollection)、typeof(ListUserControl)、new PropertyMetadata(null));
公共字符串BindingPropertyPath
{
获取{return(string)GetValue(BindingPropertyPathProperty);}
set{SetValue(BindingPropertyPathProperty,value);}
}
//使用DependencyProperty作为BindingPropertyPath的备份存储。这将启用动画、样式、绑定等。。。
公共静态只读DependencyProperty BindingPropertyPathProperty=
Register(“BindingPropertyPath”、typeof(string)、typeof(ListUserControl)、newPropertyMetadata(string.Empty));
公共ListUserControl()
{
this.InitializeComponent();
}
私有void FrameworkElement_已加载(对象发送方,路由目标)
{
按钮按钮=发送器为按钮;
Binding contentBinding=新绑定();
contentBinding.Path=新属性路径(this.BindingPropertyPath);
button.SetBinding(button.ContentProperty,contentBinding);
}
}
现在,我们将usercontrol添加到主页面(ListPageHost):

编辑:将新的依赖属性BindingPropertyPath设置为要用于按钮的ItemsSource属性的名称

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Button Grid.Row="0" Content="Add Item to ItemsSource" Click="ButtonBase_OnClick"></Button>
    <local:ListUserControl Grid.Row="1" UserControlItemsSource="{Binding SampleCollection}" BindingPropertyPath="Title"/>
</Grid>

在主页的代码隐藏中,我们声明主页视图模型(ListPageHostViewModel):

公共类ListPageHostViewModel
{
私有只读ObservableCollection _sampleCollection=新ObservableCollection();
公共可观测采集样本采集
{
获取{return\u sampleCollection;}
}
}
。。。以及主页的(ListPageHost)代码隐藏:

public sealed partial class ListPageHost : Page
{
    public ListPageHost()
    {
        this.InitializeComponent();
        this.DataContext = new ListPageHostViewModel();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var vm = this.DataContext as ListPageHostViewModel;
        if (vm != null)
        {
            vm.SampleCollection.Add(new SampleModel() { Title = string.Format("new item {0}", DateTime.Now.Ticks)});
        }
    }
}
公共密封部分类ListPageHost:第页
{
公共ListPageHost()
{
this.InitializeComponent();
this.DataContext=新的ListPageHostViewModel();
}
/// 
///当此页面即将显示在框架中时调用。
/// 
///描述如何访问此页的事件数据。参数
///属性通常用于配置页面。
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
}
private void按钮base_OnClick(对象发送方,RoutedEventTarget e)
{
var vm=this.DataContext作为ListPageHostViewModel;
if(vm!=null)
{
添加(新的SampleModel(){Title=string.Format(“新项{0}”,DateTime.Now.Ticks)});
}
}
}
希望这就是你要找的。如果你有任何问题,请告诉我。
你好,亚历克斯你好,亚历克斯。。谢谢你的回复。样品非常接近我要找的,但需要更多帮助。嗨,亚历克斯。。谢谢你的回复。样品非常接近我要找的,但不需要更多的帮助。在上面的示例中,您已经在UserControl的按钮内容和主页SampleCollection的SampleModel类型中使用了Title属性,但我想为Title创建一个依赖属性,以便UserControl的客户端可以映射到依赖属性。我不想让UserControl和主页了解标题属性。我只是想让usercontrol松散耦合。这是可以实现的吗??请让我知道。非常感谢你的帮助。真是帮了大忙。我很快就会看你的问题。。。我现在不在办公室。所以你想通过依赖属性来决定按钮文本应该使用哪个属性?我更新了我的答案!请让我知道,如果那是你想要的。。。中国
public sealed partial class ListPageHost : Page
{
    public ListPageHost()
    {
        this.InitializeComponent();
        this.DataContext = new ListPageHostViewModel();
    }

    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.  The Parameter
    /// property is typically used to configure the page.</param>
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
    }

    private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
    {
        var vm = this.DataContext as ListPageHostViewModel;
        if (vm != null)
        {
            vm.SampleCollection.Add(new SampleModel() { Title = string.Format("new item {0}", DateTime.Now.Ticks)});
        }
    }
}