2012-11-16 164 views
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; 
    } 


} 
} 

主。 XAML

<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> 

謝謝!

+0

你是什麼地方設置的DataContext? –

+0

什麼是DataContext,它是如何工作的? – Samurai336

+0

查看原始問題 –

回答

0

您需要在某處設置DataContext。沒有它 - 你的綁定沒有上下文,並且失敗(當調試Debug時,查看VS中的Output窗口以查看綁定錯誤)。您正在設置ItemsSource =「{Binding}」,其中綁定沒有上下文。

然後再次 - 您正在使用您的「ListOfAUserControls.ItemsSource = dataAndStuff;」重寫其他位置的ItemsSource。電話,這可能不是導致您的問題到最後的問題。 ItemsControl中項目的DataContext應自動設置爲ItemsSource集合中的項目。

可能會阻止你的另一個問題則存在someKindaOfDataHolder.Text1你的情況是一個字段,它應該是綁定的工作屬性 - 試試這個來代替:

public string Text1 { get; set; } 
+0

誤讀你在哪裏談論完全工作感謝的傢伙。 – Samurai336

相關問題