2017-12-18 153 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

完美 –