有没有办法使用 c# 为 Windows 8 商店应用程序缩进列表视图中的某些项目?

Posted

技术标签:

【中文标题】有没有办法使用 c# 为 Windows 8 商店应用程序缩进列表视图中的某些项目?【英文标题】:Is there a way to to Indent certain items in a list view using c# for a windows 8 store app? 【发布时间】:2015-02-15 03:36:20 【问题描述】:

基本上,我有一个项目和子项目的列表(都在一个列表中)。我想缩进某些实际上是子项目的特定项目。是否有可以用来执行此操作的函数或属性?我试过用谷歌搜索它,甚至在堆栈溢出时在这里搜索它——但没有成功。

注意:我将 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>

列表视图绑定到一个可观察的对象列表。

【问题讨论】:

您的代码似乎有问题。但是,除非我们有code or information that can reproduce the problem,否则我们无能为力。否则,我们只是在盲目猜测。 @gunr2171,它只是使用 XAML 和 C# 的列表视图的通用代码。但我将很快更新一个代码示例 @gunr2171 我刚刚进行了编辑以显示一些 XAML 代码。 我只是希望能够缩进特定的项目,这些项目是可观察类中的对象,并且具有用于缩进的整数属性。 另一个选项是根据您想要的 UI 外观对列表项进行分组:msdn.microsoft.com/en-us/library/windows/apps/xaml/… 【参考方案1】:

在这里进一步扩展另一个答案...建议的路线是向基础类(在本例中为 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; 

您还需要一个很容易设置的 Converter 类: 你需要一个转换器类;一旦你掌握了它们的窍门,转换器就真的很容易了。这种情况的一个简单示例是:

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>

【讨论】:

这是我正在寻找的示例的确切类型。不过,在我的项目中,我的转换器类是否会位于 Common 文件夹或 Models 文件夹中?因为它不是 Page 类 或者转换器类是可移植类吗? 我的项目中通常有几个转换器,所以我创建了一个“转换器”文件夹让它们都进入。转换器可以只是该文件夹中的一个类(请注意,您需要使用标准的右键单击 -> 添加新项目 -> 类更新命名空间以还包括 MyNamespace.Converters 以便项目能够在 XAML 中引用它们)此外,您可能需要将以下内容添加到您的类中:使用系统;使用 Windows.UI.Xaml.Data; 我能弄明白,谢谢你的信息,不过最后一个问题。我已经添加了这些命名空间,system 和 Xaml.Data,但是当我返回新的厚度时它无法识别厚度。 Thickness 使用什么命名空间? 找到了,是Windows.UI.Xaml(没有数据部分)【参考方案2】:

如果您只想缩进子项目的文本,您可以更改文本块或堆栈面板的边距。 为此,您需要执行以下操作:

    为要添加到列表视图的项目创建一个类

    添加到类中,IsSubItem 属性

    创建这些项目的可观察集合并将它们绑定到您的列表视图源。

    在 ListView 模板中,使用转换器将堆栈面板或文本块边距绑定到 IsSubItem 属性,以将 IsSubItem 布尔值转换为适当的边距。

【讨论】:

我有一个具有 indent 属性的类的可观察列表。所以我想此时我只需要实现转换器。仅供参考,您能否制作有关如何实现转换器的通用代码?谢谢。 另外,我需要创建一个转换器类吗?或者我可以将边距绑定设置为页面类定义中的私有函数吗? 我刚刚看到您的其他问题 - 我很高兴看到有人已经回答了这些问题 - 看起来您正在路上。

以上是关于有没有办法使用 c# 为 Windows 8 商店应用程序缩进列表视图中的某些项目?的主要内容,如果未能解决你的问题,请参考以下文章

Windows 应用商店应用程序 - 选择文本并点击

如何制作一个 C# Windows 窗体应用程序,为 Windows 应用商店转换,从 Windows 开始

为 Windows 应用商店应用程序创建包的脚本

为 Windows 商店应用程序打开 Web 服务?

Windows 8 商店即时消息应用程序 [关闭]

Windows 8.1商店应用的信用卡支付实现[关闭]