我正在嘗試創建具有可變數量的行和列的表。我有這樣做ItemsControl具有Grid作為ItemsPanel。而且我知道我可以通過它設置Grid.Row和設置Grid.Column每個項目ItemContainerStyle。但是當我無法通過網格名稱訪問網格時,我不知道如何更改行和列的數量及其大小。題:你如何修改RowDefinitions或ColumnDefinitions的Grid使用只在運行時XAML和結合與無代碼隱藏?這是XAML代碼:<ItemsControl Name="myItemsControl" ItemsSource="{Binding Cells}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Grid Name="myGrid"> <Grid.RowDefinitions> <!-- unknown number of rows are added here in run-time --> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <!-- known number of columns are added here in run-time --> </Grid.ColumnDefinitions> </Grid> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemContainerStyle> <Style.../> </ItemsControl.ItemContainerStyle></ItemsControl>我試圖RowDefinition在代碼后面添加一些內容,但myGrid由于它位于內,因此找不到以其名稱(或任何其他方式)訪問的方法ItemsPanelTemplate。我想知道是否有任何方法可以在運行時以編程方式添加或修改RowDefinitions?
3 回答

慕雪6442864
TA貢獻1812條經驗 獲得超5個贊
您可以將附加屬性用于Grid修改,RowDefinitions以及ColumnDefinitions在設置或更改這些屬性時使用。
它將允許您這樣寫Grid:
<Grid local:GridHelpers.RowCount="{Binding MaxGridRow}"
local:GridHelpers.ColumnCount="3" />
然后,僅公開您的屬性,ViewModel該屬性將返回Cells集合中最大的行號。
您可以在我的博客中找到這些屬性的詳細實現。
添加回答
舉報
0/150
提交
取消