GridView 模板绑定 (C# UWP)
GridView Template Binding (C# UWP)
我正在解决有关绑定的问题,但到目前为止还没有找到解决我的问题的方法(这可能是一个愚蠢的问题,但我对 UWP 真的很陌生)。
我试图在 GridView 对象中添加我的应用程序创建的 "projects",因此我为其创建了模板:
<GridView x:Name="GV_Seznam_Projektov" Grid.Column="2" Margin="0,10,0,0" Grid.Row="2">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid Background="#19162D50" HorizontalAlignment="Left" Width="200" Height="300" VerticalAlignment="Top">
<Rectangle Fill="{StaticResource cl_main_back}" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Height="60" Margin="0,40,0,0" TextWrapping="Wrap" Text="{x:Bind}" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0,15,0,0" FontSize="20" FontWeight="Light"/>
<TextBlock HorizontalAlignment="Left" Height="20" Margin="0,80,0,0" TextWrapping="Wrap" Text="by Laurent Resman" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0" FontSize="12" FontWeight="Light"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
并像这样添加对象:
GV_Seznam_Projektov.Items.Add("fublisher.io");
我在 msdn 网络上找到了所有关于它的文档,但后来我遇到了一个问题,我希望每个对象都有不同的 "top bar" 和背景颜色,所以有谁知道如何绑定,在我的例如模板中的三个不同变量?我猜我应该做一个对象,对吧?但我不知道如何正确绑定它。
感谢阅读所有这些:)
An ItemsControl object can serve two roles. It can be used to present a fixed set of items, or it can be used to display a list obtained from data binding to a data source. Using an ItemsControl for data is more common. To display data, specify the binding to the data as the ItemsSource value (or use the data context) and don't populate Items. If you want to display a fixed list, populate Items with one or more FrameworkElement child objects, and don't specify ItemsSource.
ItemsSource typically references a list of items. This can be a fixed list from a business object, or a list that's designed to fire notifications if the underlying data changes. The list might be a generic interface (for example IList) or a practical class that implements the collection interfaces that Windows Runtime data binding supports. When you display items in an ItemsControl, you can use the ItemTemplate property, the ItemsPanel property, or both to specify the appearance of the items.
详情请见ItemsControl.
的备注
在您的代码中,您将“fublisher.io”设置为 GridView
的 Items
。您应该能够将它添加到列表并将列表设置为 GridView
.
的 ItemsSource
例如:
<GridView x:Name="GV_Seznam_Projektov" Grid.Column="2" Margin="0,10,0,0" Grid.Row="2" ItemsSource="{x:Bind Items}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid Background="#19162D50" HorizontalAlignment="Left" Width="200" Height="300" VerticalAlignment="Top">
<Rectangle Fill="{StaticResource cl_main_back}" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="200" />
<TextBlock HorizontalAlignment="Left" Height="60" Margin="0,40,0,0" TextWrapping="Wrap" Text="{x:Bind}" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0,15,0,0" FontSize="20" FontWeight="Light" />
<TextBlock HorizontalAlignment="Left" Height="20" Margin="0,80,0,0" TextWrapping="Wrap" Text="by Laurent Resman" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0" FontSize="12" FontWeight="Light" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
后面的代码:
private ObservableCollection<string> Items;
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<string>();
Items.Add("fublisher.io");
}
我正在解决有关绑定的问题,但到目前为止还没有找到解决我的问题的方法(这可能是一个愚蠢的问题,但我对 UWP 真的很陌生)。 我试图在 GridView 对象中添加我的应用程序创建的 "projects",因此我为其创建了模板:
<GridView x:Name="GV_Seznam_Projektov" Grid.Column="2" Margin="0,10,0,0" Grid.Row="2">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid Background="#19162D50" HorizontalAlignment="Left" Width="200" Height="300" VerticalAlignment="Top">
<Rectangle Fill="{StaticResource cl_main_back}" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="200"/>
<TextBlock HorizontalAlignment="Left" Height="60" Margin="0,40,0,0" TextWrapping="Wrap" Text="{x:Bind}" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0,15,0,0" FontSize="20" FontWeight="Light"/>
<TextBlock HorizontalAlignment="Left" Height="20" Margin="0,80,0,0" TextWrapping="Wrap" Text="by Laurent Resman" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0" FontSize="12" FontWeight="Light"/>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
并像这样添加对象:
GV_Seznam_Projektov.Items.Add("fublisher.io");
我在 msdn 网络上找到了所有关于它的文档,但后来我遇到了一个问题,我希望每个对象都有不同的 "top bar" 和背景颜色,所以有谁知道如何绑定,在我的例如模板中的三个不同变量?我猜我应该做一个对象,对吧?但我不知道如何正确绑定它。
感谢阅读所有这些:)
An ItemsControl object can serve two roles. It can be used to present a fixed set of items, or it can be used to display a list obtained from data binding to a data source. Using an ItemsControl for data is more common. To display data, specify the binding to the data as the ItemsSource value (or use the data context) and don't populate Items. If you want to display a fixed list, populate Items with one or more FrameworkElement child objects, and don't specify ItemsSource.
ItemsSource typically references a list of items. This can be a fixed list from a business object, or a list that's designed to fire notifications if the underlying data changes. The list might be a generic interface (for example IList) or a practical class that implements the collection interfaces that Windows Runtime data binding supports. When you display items in an ItemsControl, you can use the ItemTemplate property, the ItemsPanel property, or both to specify the appearance of the items.
详情请见ItemsControl.
的备注在您的代码中,您将“fublisher.io”设置为 GridView
的 Items
。您应该能够将它添加到列表并将列表设置为 GridView
.
ItemsSource
例如:
<GridView x:Name="GV_Seznam_Projektov" Grid.Column="2" Margin="0,10,0,0" Grid.Row="2" ItemsSource="{x:Bind Items}">
<GridView.ItemTemplate>
<DataTemplate x:DataType="x:String">
<Grid Background="#19162D50" HorizontalAlignment="Left" Width="200" Height="300" VerticalAlignment="Top">
<Rectangle Fill="{StaticResource cl_main_back}" HorizontalAlignment="Left" Height="40" VerticalAlignment="Top" Width="200" />
<TextBlock HorizontalAlignment="Left" Height="60" Margin="0,40,0,0" TextWrapping="Wrap" Text="{x:Bind}" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0,15,0,0" FontSize="20" FontWeight="Light" />
<TextBlock HorizontalAlignment="Left" Height="20" Margin="0,80,0,0" TextWrapping="Wrap" Text="by Laurent Resman" VerticalAlignment="Top" Width="200" TextAlignment="Center" Padding="0" FontSize="12" FontWeight="Light" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
后面的代码:
private ObservableCollection<string> Items;
public MainPage()
{
this.InitializeComponent();
Items = new ObservableCollection<string>();
Items.Add("fublisher.io");
}