C# Windows Phone 8.1项目中的ItemsControl
有必要在某些单元格中排列元素,但它们都位于左上角的单元格中。由于行和列的动态创建,这种想法不起作用,但在静态广告中,没有任何变化 请帮忙 XAMLC# Windows Phone 8.1项目中的ItemsControl,c#,xaml,windows-phone-8.1,itemscontrol,C#,Xaml,Windows Phone 8.1,Itemscontrol,有必要在某些单元格中排列元素,但它们都位于左上角的单元格中。由于行和列的动态创建,这种想法不起作用,但在静态广告中,没有任何变化 请帮忙 XAML 不幸的是,由于容器不在XAML中,因此没有可绑定的内容,因此这将不起作用。您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示: public class CustomItemsControl : ItemsControl { protected override vo
不幸的是,由于容器不在XAML中,因此没有可绑定的内容,因此这将不起作用。您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示:
public class CustomItemsControl : ItemsControl
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(Grid.ColumnProperty, new Binding { Path = new PropertyPath("Col") });
source.SetBinding(Grid.RowProperty, new Binding { Path = new PropertyPath("Row") });
}
}
然后将XAML更改为CustomItemsControl,而不是items控件:
<local:CustomItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}" />
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>-->
</local:CustomItemsControl>
不幸的是,由于容器不在XAML中,因此没有可绑定的内容,因此这将不起作用。您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示:
public class CustomItemsControl : ItemsControl
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(Grid.ColumnProperty, new Binding { Path = new PropertyPath("Col") });
source.SetBinding(Grid.RowProperty, new Binding { Path = new PropertyPath("Row") });
}
}
然后将XAML更改为CustomItemsControl,而不是items控件:
<local:CustomItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}" />
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>-->
</local:CustomItemsControl>
不幸的是,由于容器不在XAML中,因此没有可绑定的内容,因此这将不起作用。您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示:
public class CustomItemsControl : ItemsControl
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(Grid.ColumnProperty, new Binding { Path = new PropertyPath("Col") });
source.SetBinding(Grid.RowProperty, new Binding { Path = new PropertyPath("Row") });
}
}
然后将XAML更改为CustomItemsControl,而不是items控件:
<local:CustomItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}" />
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>-->
</local:CustomItemsControl>
不幸的是,由于容器不在XAML中,因此没有可绑定的内容,因此这将不起作用。您需要做的是将ItemsControl子类化并重写PrepareContainerForItemOverride函数,如下所示:
public class CustomItemsControl : ItemsControl
{
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
base.PrepareContainerForItemOverride(element, item);
FrameworkElement source = element as FrameworkElement;
source.SetBinding(Grid.ColumnProperty, new Binding { Path = new PropertyPath("Col") });
source.SetBinding(Grid.RowProperty, new Binding { Path = new PropertyPath("Row") });
}
}
然后将XAML更改为CustomItemsControl,而不是items控件:
<local:CustomItemsControl x:Name="ItControl" ItemTemplate="{StaticResource DefaultGridItemTemplate}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!--<ItemsControl.ItemContainerStyle>
<Style TargetType="ContentPresenter">
<Setter Property="Grid.Column" Value="{Binding Col}" />
<Setter Property="Grid.Row" Value="{Binding Row}"/>
</Style>
</ItemsControl.ItemContainerStyle>-->
</local:CustomItemsControl>
尝试在StackPanel内添加方向垂直的主项控件
谢谢尝试在StackPanel中添加方向垂直的主项控件
谢谢尝试在StackPanel中添加方向垂直的主项控件
谢谢尝试在StackPanel中添加方向垂直的主项控件 谢谢