gpt4 book ai didi

silverlight - WIndows Phone 中的 Grid 和 ItemsControl.ItemContainerStyle

转载 作者:行者123 更新时间:2023-12-04 02:40:27 26 4
gpt4 key购买 nike

我有一个严重的问题,我无法解决它。以下代码在 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) 中实现。但是项目面板模板网格的所有元素都在彼此之上,我的意思是在相同的 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 绑定(bind))。你有什么建议我做错了什么或者我应该使用什么?我必须严格使用 MVVM,所以没有代码隐藏。任何建议都会得到感谢和抱歉我的英语:)

最佳答案

您的想法是正确的 - 您确实需要在 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}"

关于silverlight - WIndows Phone 中的 Grid 和 ItemsControl.ItemContainerStyle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15212855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com