<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
</Grid>
我想TextBlock在這里添加串列中的。

視圖模型代碼

我嘗試使用ListBox,但這不是我想要的。
<ListBox ItemsSource="{Binding Serials}" Grid.Row="1">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" IsItemsHost="True" ScrollViewer.CanContentScroll="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock Text="{Binding aaa}" Margin="20"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

我將框出網格并進行分頁。我想要的是TextBlock在網格中居中。
我該怎么做?
uj5u.com熱心網友回復:
這里的方法是錯誤的,你可以簡單地使用ListView代替網格,它會動態地使用空間并在網格視圖中顯示。
我們填充我們自己的 User 物件的串列,每個用戶都有一個名字和一個年齡。一旦我們將串列分配給 ListView 的 ItemsSource 屬性,資料系結程序就會自動發生,但結果有點令人沮喪:
這是示例代碼:
檔案:
<Grid>
<ListView Margin="10" Name="lvDataBinding"></ListView>
</Grid>
C#
public partial class ListViewDataBindingSample : Window
{
public ListViewDataBindingSample()
{
InitializeComponent();
List<User> items = new List<User>();
items.Add(new User() { Name = "John Doe", Age = 42 });
items.Add(new User() { Name = "Jane Doe", Age = 39 });
items.Add(new User() { Name = "Sammy Doe", Age = 13 });
lvDataBinding.ItemsSource = items;
}
}
public class User
{
public string Name { get; set; }
public int Age { get; set; }
}
uj5u.com熱心網友回復:
我認為您在Grid和之間感到困惑Listview。
為什么要同時使用兩者?
嘗試水平串列視圖。
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"></StackPanel>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
uj5u.com熱心網友回復:
如果你想系結在一個串列Grid一樣的布局,使用ListBox與UniformGrid作為專案小組。在 中指定一個屬性DisplayMemberPath,那么您就不必創建多余的DataTemplate來系結了aaa。分配ItemsContainerStyle以調整內容對齊以將內容居中。
<ListBox Grid.Row="1" ItemsSource="{Binding Serials}" DisplayMemberPath="aaa">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="6" Columns="8"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
當然你也可以系結的Rows和Columns的UniformGrid,動態地適應電網。
uj5u.com熱心網友回復:
XAML 代碼
<Grid Grid.Row="1" x:Name="gridMain">
<Grid.ColumnDefinitions>
...
</Grid>
背后的代碼
TextBlock textBlock = new TextBlock();
textBlock.Text = "I'm here!";
Grid.SetRow(textBlock, 2);
Grid.SetColumn(textBlock, 2);
gridMain.Children.Add(textBlock);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376948.html
