我有一个严重的问题,我无法解决。以下代码在 WPF 中完美运行:
<ItemsControl Grid.Row="1" ItemsSource="{Binding GameFields}" HorizontalAlignment="Stretch">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="7" Columns="7" Margin="20" HorizontalAlignment="Center" VerticalAlignment="Center" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding SingleStepCommand}" CommandParameter="{Binding Index}" Visibility="{Binding Visible}" Background="Transparent"
Margin="10" BorderBrush="Transparent" BorderThickness="0">
<Button.Content>
<Image Source="{Binding IconPath}" Stretch="Fill" />
</Button.Content>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Grid.Row" Value="{Binding X}" />
<Setter Property="Grid.Column" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
并尝试在 Windows Phone(7.1) 中实现。但是 itemspaneltemplate 网格的所有元素都在彼此上,我的意思是在相同的 X 和 Y 位置上。这是我尝试过的:
<ItemsControl x:Name="ContentSource" Grid.Row="1" Margin="12,0,12,0" ItemsSource="{Binding GameFields}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Grid.RenderTransform>
<TransformGroup>
<TranslateTransform X="{Binding X}" Y="{Binding Y}" />
</TransformGroup>
</Grid.RenderTransform>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button Command="{Binding SingleStepCommand}" CommandParameter="{Binding Index}" Visibility="{Binding Visible}" Grid.Row="{Binding X}"
Grid.Column="{Binding Y}">
<Button.Content>
<Image Source="{Binding IconPath}" Stretch="Fill" />
</Button.Content>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!--ItemsControl.Resources>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Row" Value="17" />
<Setter Property="Grid.Column" Value="17" />
</Style>
</ItemsControl.Resources-->
</ItemsControl>
3种不同的方式,它们都不起作用。(资源、TranslateTransform 和 Button grid.row 绑定)。你有什么建议我做错了什么或者我应该使用什么?我必须严格使用 MVVM,所以后面没有代码。任何建议都会对我的英语表示感谢和抱歉:)