3

我有一个严重的问题,我无法解决。以下代码在 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,所以后面没有代码。任何建议都会对我的英语表示感谢和抱歉:)

4

2 回答 2

4

您有正确的想法 - 您确实需要在 ContentPresenter 上设置 Grid.Row 和 Grid.Column 属性。最简单的方法是使用您自己的每个附加属性。

像这样的东西:

public class ItemsGridLayout
{
    public static int GetGridRow(DependencyObject obj)
    {
        return (int)obj.GetValue(GridRowProperty);
    }

    public static void SetGridRow(DependencyObject obj, int value)
    {
        obj.SetValue(GridRowProperty, value);
    }

    // Using a DependencyProperty as the backing store for GridRow.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridRowProperty =
        DependencyProperty.RegisterAttached("GridRow", typeof(int), typeof(FrameworkElement), new PropertyMetadata(0, (s, e) =>
        {
            var presenter = GetItemsPresenter(s);
            if (presenter != null)
            {
                Grid.SetRow(presenter, GetGridRow(s));
            }
        }));

    public static int GetGridColumn(DependencyObject obj)
    {
        return (int)obj.GetValue(GridColumnProperty);
    }

    public static void SetGridColumn(DependencyObject obj, int value)
    {
        obj.SetValue(GridColumnProperty, value);
    }

    // Using a DependencyProperty as the backing store for GridColumn.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridColumnProperty =
        DependencyProperty.RegisterAttached("GridColumn", typeof(int), typeof(FrameworkElement), new PropertyMetadata(0, (s, e) =>
        {
            var presenter = GetItemsPresenter(s);
            if (presenter != null)
            {
                Grid.SetColumn(presenter, GetGridColumn(s));
            }
        }));

    static FrameworkElement GetItemsPresenter(DependencyObject target)
    {
        while (target != null)
        {
            if (target is ContentPresenter)
            {
                return target as FrameworkElement;
            }
            target = VisualTreeHelper.GetParent(target);
        }
        return null;
    }
}

然后在 XAML 中它是这样使用的:

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button local:ItemsGridLayout.GridRow="{Binding X}" 
                    local:ItemsGridLayout.GridColumn="{Binding Y}" 
于 2013-03-05T19:05:16.617 回答
1

我今天遇到了同样的问题。在WPF之后学习Silverlight,没想到行不通。

在进行了几次搜索以避免这种变通方法后,我尝试为它添加一个Style资源,ContentPresenter它就像 Silverlight 5 中的魅力一样。

    <ItemsControl ItemsSource="{Binding Source={StaticResource CVM}, Path=PagedItems}">
        <ItemsControl.Resources>
            <Style TargetType="ContentPresenter">
                <Setter Property="Grid.Column" Value="{Binding OffsetX}"/>
                <Setter Property="Grid.Row" Value="{Binding OffsetY}"/>
            </Style>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        ...
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        ...
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
    </ItemsControl>
于 2014-09-25T12:38:37.800 回答