1

有人能告诉我为什么我在这里绑定数据的方式不起作用吗?我可以通过使用 GridView 来做到这一点。我不确定为什么数据没有在这里传递给用户控件,此外它编译得很好并显示带有默认文本的用户控件

Main.xaml.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace ItemsControlSample
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();
    }

    /// <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)
    {
        PopulateData(); 

    }

    public class someKindaOfDataHolder
    {
        public string Text1;
        public string Text2; 
    }

    private void PopulateData()
    {
        List<someKindaOfDataHolder> dataAndStuff = new List<someKindaOfDataHolder>(); 

        for (int i = 0; i < 5; ++i)
        {
            dataAndStuff.Add(new someKindaOfDataHolder()
            {
                Text1 = "data spot 1: " + i.ToString(),
                Text2 = "data spot 2: " + (i + 2).ToString() 
            }); 

        }

        ListOfAUserControls.ItemsSource = dataAndStuff; 
    }


}
}

主文件

<Page
x:Class="ItemsControlSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ItemsControlSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <ScrollViewer>
            <ItemsControl x:Name="ListOfAUserControls" ItemsSource="{Binding}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                        <local:CustomUserControl DynamicText1Text="{Binding Text1}"  DynamicText2Text="{Binding Text2}" Margin="0,0,10,0"/> 
                    </DataTemplate>
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,46,-50,0"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

        </ItemsControl>
    </ScrollViewer>


</Grid>
</Page>

CustomUserControl.xaml.cs:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace ItemsControlSample
{
public sealed partial class CustomUserControl : UserControl
{


    public static readonly DependencyProperty DynamicText1Property =
     DependencyProperty.Register("DynamicText1Text", typeof(string), typeof(CustomUserControl), new PropertyMetadata(null, OnDynamicText1PropertyChanged));

    public string DynamicText1Text
    {
        get { return (string)GetValue(DynamicText1Property); }
        set { SetValue(DynamicText1Property, value); }
    }

    public static readonly DependencyProperty DynamicText2Property =
     DependencyProperty.Register("DynamicText2Text", typeof(string), typeof(CustomUserControl), new PropertyMetadata(null, OnDynamicText2PropertyChanged));

    public string DynamicText2Text
    {
        get { return (string)GetValue(DynamicText2Property); }
        set { SetValue(DynamicText2Property, value); }
    }

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

    private static void OnDynamicText1PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var obj = d as CustomUserControl;
        obj.DynamicText1.Text = e.NewValue.ToString();
    }

    private static void OnDynamicText2PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var obj = d as CustomUserControl;
        obj.DynamicText2.Text = e.NewValue.ToString();
    }
}
}

CustomUserControl.xaml:

<UserControl
x:Class="ItemsControlSample.CustomUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ItemsControlSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="800"
d:DesignWidth="800">

<Grid Background="Blue">
    <Grid.RowDefinitions>
        <RowDefinition Height="6*"/>
        <RowDefinition Height="2*"/>
    </Grid.RowDefinitions>

    <Image  Source="ms-appx:///Assets/MSIcon.png" />
    <StackPanel Grid.Row="1"   Margin="25">
        <TextBlock Text="Obay" FontSize="45" />
        <TextBlock x:Name="DynamicText1" Text="DynamicText1" FontSize="35" />
        <TextBlock x:Name="DynamicText2" Text="DynamicText2" FontSize="35" />
    </StackPanel>
</Grid>
</UserControl>

谢谢!

4

1 回答 1

0

您需要在某处设置DataContext 。没有它 - 您的绑定没有上下文并且它们失败(在调试 Debug 构建时查看 VS 中的输出窗口以查看绑定错误)。您正在设置 ItemsSource="{Binding}" 绑定没有上下文。

再说一遍 - 你正在用你的“ListOfAUserControls.ItemsSource = dataAndStuff;”覆盖其他地方的 ItemsSource 打电话,所以这可能不是最终导致您的问题的问题。ItemsControl 中项目的 DataContext 应自动设置为 ItemsSource 集合中的项目。

另一个可能会阻止你的问题是 someKindaOfDataHolder.Text1 在你的情况下是一个字段,它应该是一个与绑定一起使用的属性 - 试试这个:

public string Text1 { get; set; }
于 2012-11-16T18:09:17.223 回答