2017-12-18 248 views
1

以下是我在网格视图中的物料模板。在网格可观察集合中显示网格物料位置编号

     </Grid.ColumnDefinitions> 
         <StackPanel> 
          <StackPanel Orientation="Horizontal"> 
           <TextBlock Text="{Binding SerialNumber}" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Margin="10" FontSize="25"/> 
           <TextBlock Text="." VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" FontSize="25" /> 
          </StackPanel> 
          <Image Grid.Column="0" Margin="20" Height="100" Width="150" HorizontalAlignment="Center" Source="{Binding ImageUri,Mode=TwoWay}" VerticalAlignment="Center"/> 
         </StackPanel> 
       </Border> 
      </DataTemplate> 
     </GridView.ItemTemplate> 

什么,我想实现的是显示在TextBlock的文本集合中的项目位置=“{结合的SerialNumber}(如果这是一个列表视图,它会。将行号),请我如何做到这一点

回答

0

你只需要定义一个类与特定的属性,并将其绑定在XAML

我做了一个简单的代码示例,供您参考:

<GridView ItemsSource="{Binding oc}"> 
     <GridView.ItemTemplate> 
      <DataTemplate> 
       <Border> 
        <StackPanel> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding SerialNumber}" VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" Margin="10" FontSize="25"/> 
          <TextBlock Text="." VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Bold" FontSize="25" /> 
         </StackPanel> 
         <Image Grid.Column="0" Margin="20" Height="100" Width="150" HorizontalAlignment="Center" Source="{Binding source,Mode=TwoWay}" VerticalAlignment="Center"/> 
        </StackPanel> 
       </Border> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
</GridView> 
public sealed partial class MainPage : Page 
{ 
    public ObservableCollection<Test> oc { get; set;} 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     oc = new ObservableCollection<Test>(); 
     oc.CollectionChanged += Oc_CollectionChanged; 
     for (int i=0;i<10;i++) 
     { 
      oc.Add(new Test() {SerialNumber=i,source=new BitmapImage(new Uri("ms-appx:///Assets/test.png")) }); 
     } 
     this.DataContext = this; 
    } 

    private void Oc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) 
    { 
     if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Remove) 
     { 
      for (int i =e.OldStartingIndex; i<oc.Count;i++) 
      { 
       oc[i].SerialNumber = i; 
      } 
     } 
    } 
} 


public class Test:INotifyPropertyChanged 
{ 
    private int _SerialNumber; 
    public int SerialNumber 
    { 
     get { return _SerialNumber; } 
     set 
     { 
      _SerialNumber = value; 
      RaisePropertyChanged("SerialNumber"); 
     } 
    } 

    private BitmapImage _source; 
    public BitmapImage source 
    { 
     get { return _source; } 
     set 
     { 
      _source = value; 
      RaisePropertyChanged("source"); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    private void RaisePropertyChanged(string PropertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this,new PropertyChangedEventArgs(PropertyName)); 
     } 
    } 
} 
+0

@泽维尔谢,做ü提供帮助更新时,曾经的项目是从观察集合删除的ObservableCollection行号解决?如果没有的话,关于如何去做的任何想法。 –

+0

如果你删除了项目,你需要自己更新'SerialNumber'。请参阅我的更新回复。我注册了'CollectionChanged'事件来更新SerialNumber,并使'Test'类继承自[INotifyPropertyChanged](https://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged(v = vs。 110)的.aspx)。它用于通知UI SerialNumber已更新。 –

+0

完美 –