2012-08-25 41 views
0

我试图在我的Metro应用程序中使用C#和XAML SemanticZoom控件进行绑定,但老实说我对如何做到这一点不知所措。这里是XAML代码中,我通过问题,文章等一起拼凑至今得到:在C#和XAML中绑定数据?

<SemanticZoom x:Name="boardZoom" Height="626" Margin="10,132,10,0" VerticalAlignment="Top"> 
     <SemanticZoom.ZoomedInView> 

      <GridView IsSwipeEnabled="True" x:Name="ItemsGridView"> 

        <GridView.ItemTemplate> 

         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Margin="10,10,0,0" 
         HorizontalAlignment="Left" Background="White"> 
           <TextBlock Text="{Binding Title}" TextWrapping="Wrap" Width="200" Height="300" 
            FontFamily="Global User Interface" FontSize="40" Foreground="Black" 
          VerticalAlignment="Center" HorizontalAlignment="Left"/> 

           <Image Source="{Binding Image}" Height="60" Width="60" 
         VerticalAlignment="Center" Margin="0,0,10,0" Visibility="{Binding isImage}" /> 

           <TextBlock Text="{Binding Category}" TextWrapping="Wrap" Width="200" 
            FontFamily="Global User Interface" Foreground="Black" 
          VerticalAlignment="Center" HorizontalAlignment="Left"/> 
          </StackPanel> 
         </DataTemplate> 

        </GridView.ItemTemplate> 

       </GridView> 
      </SemanticZoom.ZoomedInView> 

<!--Didn't include SemanticZoom.ZoomedOutView since I'm still trying to get the ZoomedIn one working first--> 

     </SemanticZoom> 

而且我的C#代码:

 List<PinStore.pin> pins = PinStore.CopyFromStream(response.GetResponseStream()); //returns a list of PinStore.pin objects, which have name, type, Image, isImage, and Category objects 
     System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn> toSource = new System.Collections.ObjectModel.ObservableCollection<SemanticZoomed.zoomedIn>(); //should I be using ObservableCollection or something like List<> here? 
     foreach (PinStore.pin pin in pins) 
     { 
      SemanticZoomed.ZoomedIn toAdd = new SemanticZoomed.ZoomedIn(); //class with Title, Image, isImage, and Category objects 
      if (pin.type == "text") 
      { 
       toAdd.Title = pin.name; 
       toAdd.Image = null; 
       toAdd.isImage = Visibility.Collapsed; 
       toAdd.Category = pin.category; 
      } 
      toSource.Add(toAdd); 
     } 
     ItemsGridView.DataContext = toSource; 

我已经没有在XAML/C#多少经验,并没有约束力的经验。我没有收到任何错误,但我注意到如果我将ItemsGridView.DataContext = toSource;替换为ItemsGridView.ItemsSource = toSource;,GridView中会出现一个空白的堆栈面板,我似乎无法找到一种方法来填充指定的标题和类别值。谢谢!

回答

1

那么你应该首先考虑创建一个ResourceDictionary,这样你就可以保存你的项目风格。然后,您可以将ItemStyle设置为资源字典。

但无论你需要做什么:ItemsGridView.ItemsSource = toSource;如果您不选择绑定GridView xaml中的toSource。

还要确保SemanticZoomed.zoomedIn对象实现INotifyPropertyChanged接口,并正确调用该事件。并确保标题,图像,类别等是编辑时调用该事件的公共属性。而且你还需要确保pin.Text是一个实际值。 {确保}。

如果您想了解更多有关数据绑定看看他们是如何做到这一点在C#& XAML与Windows 8 {应该是相同的事情}: http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh464965.aspx

+0

感谢您的答复,我会尝试一下明天早上,回到你身边:) 一种脱离主题,但我可以问你为什么用{}而不是()?只是好奇,如果你不想回答,那么不要:D – MatthewSot

+0

我使用任何看起来很酷的符号,包括[] {}和()。我想这是因为我编程太多了,很容易按{然后(。 –

+0

啊,有道理:D – MatthewSot