C# 通用应用程序中的动态表
我正在为windows phone 8.1/windows 8.1开发通用应用程序。我使用的是MVVM模式。我有一个表,其中包含绑定到视图模型中源代码的stackpanel的动态数量。一个stackpanel是表中的一行。如何使堆栈面板中的项目具有相同的宽度?我知道C# 通用应用程序中的动态表,c#,wpf,mvvm,win-universal-app,stackpanel,C#,Wpf,Mvvm,Win Universal App,Stackpanel,我正在为windows phone 8.1/windows 8.1开发通用应用程序。我使用的是MVVM模式。我有一个表,其中包含绑定到视图模型中源代码的stackpanel的动态数量。一个stackpanel是表中的一行。如何使堆栈面板中的项目具有相同的宽度?我知道IsSharedSizeScope,但这在universal app的网格中不可用 <ItemsControl Grid.Column="1" HorizontalAlignment="Stretch"
IsSharedSizeScope
,但这在universal app的网格中不可用
<ItemsControl Grid.Column="1" HorizontalAlignment="Stretch"
ItemsSource="{Binding PowerMaxiTableSource0, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Border Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text0}" />
</Border>
<Border Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text1}" />
</Border>
<Border Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text2}" />
</Border>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
因为这是一张桌子,我试着用网格代替stackpanels,但我没能让它工作。我试过这个方法。但我最终得到了聚集在第0行、第0列的项目,即使绑定属性中的值正确
任何帮助都将不胜感激(即使是网格解决方案)。
StackPanel
s的宽度由其子级驱动。StackPanel
中似乎有3个子项,这意味着它们不是动态生成的。您声明只更改堆栈面板的数量。如果您不反对网格
使用而不是堆栈面板
,则以下代码将起作用,因为网格
将自动拉伸(采用所有允许的宽度),并在您创建宽度指定为百分比的列时动态向下延伸该宽度到其子级:
<ItemsControl HorizontalAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
<ColumnDefinition Width="33*"/>
</Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text0}" />
</Border>
<Border Grid.Column="1" Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text1}" />
</Border>
<Border Grid.Column="2" Background="{Binding IsEnabled, Converter={StaticResource BackgroundConverter}}">
<TextBlock Text="{Binding Text2}" />
</Border>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
是否可以为所有文本块指定一个固定的宽度并使用TextTrimming属性?如果没有固定的宽度会更好,因为我需要覆盖许多不同的屏幕大小,但我看不到任何其他方法。谢谢,它工作得近乎完美。然而,在某些单元格中,宽度有时会变宽,但这在我的情况下是可以接受的,谢谢。