有没有办法使用 c# 缩进列表视图中的某些项目对于 Windows 8 应用商店应用程序
本文关键字:项目 Windows 应用程序 应用 缩进 视图 列表 有没有 | 更新日期: 2023-09-27 18:34:26
基本上,我有一个项目和子项目的列表(都在一个列表中)。我想缩进某些实际上是子项目的特定项目。是否有可用于执行此操作的函数或属性?我尝试过谷歌搜索它,甚至在堆栈溢出上搜索它 - 但没有成功。
注意:我正在将 C# 和 XAML 用于 Windows 8 应用商店应用程序。不是 wpf 应用程序(文档和代码有时不同)。
编辑:
下面是我的 XAML 的外观:
<ListView
x:Name="ListView"
AutomationProperties.AutomationId="ListView"
AutomationProperties.Name="items"
TabIndex="1"
Grid.Row="1"
Margin="-10,-10,0,0"
Padding="20,0,0,45"
IsSwipeEnabled="False"
SelectionChanged="ListView_SelectionChanged">
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Margin="10,0,0,0">
<TextBlock x:Name="Item" Tag="{Binding ID}" Text="{Binding Name}" Style="{StaticResource TitleTextBlockStyle}" TextWrapping="Wrap" MaxHeight="40" FontSize="14" FontFamily="Global User Interface"/>
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
列表视图绑定到对象的可观察列表。
在这里进一步扩展另一个答案...建议的方法是将属性添加到基础类(在本例中为 MyClass),并将 ListView 的 ItemsSource 绑定到这些对象的列表。
public class MyClass
{
public bool IsSubItem { get; set; }
}
// Elsewhere in your code, you would need a list of these object
public ObservableCollection<MyClass> MyList { get; set; }
您还需要一个转换器类,它非常易于设置:您需要一个转换器类;一旦掌握了转换器的窍门,它们就真的很容易。此方案的简单示例是:
public class BoolToMarginConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
//Get the passed in value and convert it to a boolean - the as keyword returns a null if we can't convert to a boolean so the ?? allows us to set a default value if we get a null instead
bool isSubItem = (value as bool?) ?? false;
// If the item IS a sub item, we want a larger Left Margin
// Since the Margin Property expects a Thickness, that's what we need to return
return new Thickness(isSubItem == true ? 24 : 12, 0, 0, 0);
}
// This isn't necessary in most cases, and in this case can be ignored (just required for the Interface definition)
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
获得此设置后,需要将其作为资源添加到 XAML 中:
// Add a reference to your namespace if it isn't already in your XAML
xmlns:local="using:MyNamespace"
// You'll also need to add a static resource
<Page.Resources>
<local:BoolToMarginConverter x:Key="BoolToMarginConverter" />
</Page.Resources>
// And then lastly, you'll need to update your ListView XAML to use the new information
<ListView
x:Name="ListView"
ItemsSource={Binding MyList}
<!-- Other Properties removed for space -->
<ListView.ItemTemplate>
<DataTemplate>
<Grid Margin="1">
<!-- Other info removed for space -->
<StackPanel Grid.Column="0" Margin="{Binding Path=IsSubItem, Converter={StaticResource BoolToMarginConverter}}">
<!-- Other info removed for space -->
</StackPanel>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如果您只想缩进子项的文本,您可以更改文本块或堆栈面板的边距。为此,您需要执行以下操作:
-
为要添加到列表视图的项目创建一个类
-
添加到类中,IsSubItem 属性
-
创建这些项的可观察集合,并将它们绑定到列表视图源。
-
在 ListView 模板中,使用转换器将堆栈面板或文本块边距绑定到 IsSubItem 属性,以将 IsSubItem 布尔值转换为适当的边距。