我正在从SQL表中将复选框作为标签加载,但是这些复选框的方向是垂直的,这确实很丑陋。我如何将它们定向为水平?
我试图将setter属性设置为Stretch,但是它只将这些复选框居中放置在列表视图的中心,并且保持垂直。
有人可以帮助吗?
xaml:
<ListView Name="listCategory" Visibility="Visible" BorderThickness="0" HorizontalAlignment="Left" Margin="114,70,0,0" Width="207" RenderTransformOrigin="0.5,0.5" Height="180" VerticalAlignment="Top">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
CheckBox Content="{Binding tag}" IsChecked="{Binding Checked}" HorizontalAlignment="Center"/>
</DataTemplate>
</ListView.ItemTemplate>
listCategory的代码
public void loadCategoryTags()
{
DataTable dt = new DataTable();
using (SqlCommand selectTags = new SqlCommand("select tag from Categories", cs))
{
cs.Open();
using (SqlDataAdapter dataAd = new SqlDataAdapter(selectTags))
{
dt = new DataTable();
dataAd.Fill(dt);
}
cs.Close();
}
dt.Columns.Add(new DataColumn("Checked", typeof(bool)) { DefaultValue = false });
listCategory.ItemsSource = dt.DefaultView;
}
答案 0 :(得分:0)
最好不要以DataTemplate
自定义主布局,而应以ListViewItem
样式进行更改。
也就是说:
HorizontalAlignment="Center"
移除CheckBox
属性; ControlTemplate
的二传手中添加ListViewItem
的二传手,并使用HorizontalContentAlignment
属性。我知道您不是在处理HorizontalAlignment
属性,而是在处理Orientation
属性。几个面板具有Orientation
属性,您可以选择其中之一来实现所需的布局。
这是更新的代码:
<ListView Name="listCategory" Visibility="Visible" BorderThickness="0" HorizontalAlignment="Left"
Margin="114,70,0,0" Width="207" RenderTransformOrigin="0.5,0.5" Height="180" VerticalAlignment="Top">
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<!-- If you use a StackPanel, your items will be in a single line. -->
<!--<StackPanel Orientation="Horizontal" />-->
<!-- If you use a WrapPanel, your items will be wrapped into multiple lines. -->
<WrapPanel Orientation="Horizontal" />
<!-- Or you can change it to other available panels to layout the items in a specified type. -->
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding tag}" IsChecked="{Binding Checked}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>