[英]Binding Converter using index of item in list
我有一個場景,要求我將對象列表綁定到網格。 我希望這樣的行為: -
•將項目添加到列表時,它將移動到網格中的下一個可用空間。 (像一個包裹板)
下面顯示的是我目前的XAML。
<ItemsControl ItemsSource="{Binding Path=Contents}" Grid.Row="1">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<cc:DynamicGrid ShowGridLines="True" RowCount="{Binding Path= SelectedGridStyle.RowCount}" ColCount="{Binding Path=SelectedGridStyle.ColCount}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
目前,這會將所有項目放在彼此之上,因為我沒有指定行索引或列索引。 我打算添加以下內容來糾正這個問題
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Column" Value="{Binding Path=ColumnIndex}"/>
<Setter Property="Grid.Row" Value="{Binding Path=RowIndex}"/>
<Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnSpan}"/>
<Setter Property="Grid.RowSpan" Value="{Binding Path=RowSpan}"/>
</Style>
</ItemsControl.ItemContainerStyle>
我有以下代碼,這將允許我從列表中的索引計算rowIndex和columnIndex。
int m_ColumnCount = 3;
int rowIndex = index / m_ColumnCount;
int columnIndex = index - (rowIndex * m_ColumnCount);
我正在嘗試使用上面的方法創建一個綁定轉換器來設置這些值。 但是,此計算取決於視圖模型中保留的列數和行數的值。 有沒有辦法獲得這些價值?可以實現上述目標還是有更好的解決方案?
如果綁定需要多個值,則可以使用MultiBinding
。 要綁定到主視圖模型,您可以使用以ItemsControl
為目標的RelativeSource
。
看看這個 - http://www.switchonthecode.com/tutorials/wpf-tutorial-using-multibindings
您將看到大約使用多重綁定的一半,這很簡單。 然后他們會告訴你如何為它創建一個轉換器。 聽起來你熟悉轉換器,所以我認為這篇文章會讓你感動。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.