【问题标题】:How to bind muliple row result in single data binding in Windows store app in C#/XAML如何在 C#/XAML 中的 Windows 商店应用程序中绑定多行导致单个数据绑定
【发布时间】:2012-09-28 10:26:51
【问题描述】:

我的数据库表(名称:标签)是这样的

TagID       TagName
-------------------------
1       Home
2       Work
3       Office
4       Study
5       Research

我有一个列表视图,它的数据模板包含两个文本块。一个是笔记名称,另一个是标签。

lvRecentNotes.ItemsSource = db.Query<NoteDetails>("select NoteName from NoteDetails", "");

Note_Tag 表用于保存NoteIDTagID 现在我想要这样的单个文本块中的结果

Home, Study, Research

那么如何通过数据绑定来实现呢?

【问题讨论】:

    标签: c# xaml data-binding microsoft-metro windows-runtime


    【解决方案1】:

    您需要使用数据绑定值转换器。使用转换器,您可以获取行中的每个值,然后根据需要连接并返回连接后的字符串。

    参考:http://msdn.microsoft.com/en-us/library/system.windows.data.binding.converter.aspx

    代码示例: 假设您有一个名为 Note 的模型类,它具有 Name 和 Tags 参数。

    public class Note
    {
        public string Name { get; set; }
        public string Tags { get; set; }
    }
    
    public class NoteItemConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is Note)
            {
                Note note = value as Note;
    
                // Return a value based on parameter (when ConverterParameter is specified)
                if (parameter != null)
                {
                    string param = parameter as string;
                    if (param == "name")
                    {
                        return note.Name;
                    }
                    else if (param == "tags")
                    {
                        return note.Tags;
                    }
                }
    
                // Return both name and tags (when no parameter is specified)
                return string.Format("{0}: {1}", note.Name, note.Tags);
            }
    
            // Gracefully handle other types
            // Optionally, throw an exception
            return string.Empty;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return value;
        }
    }
    

    然后你可以设置你的 ItemsSource

    ObservableCollection<Note> notes = ... // Obtain your list of notes
    lvRecentNotes.ItemsSource = notes;
    

    在您的 XAML 中,将转换器添加为页面资源。

    <Page.Resources>
        <local:NoteItemConverter x:Key="NoteItemConverter"/>
    </Page.Resources>
    

    然后绑定到数据模板中的项目。

    <!-- Display only the name for the note -->
    <TextBlock Text="{Binding Item, Converter={StaticResource NoteItemConverter}, ConverterParameter=name}"/>
    
    <!-- Display only the tags for the note -->
    <TextBlock Text="{Binding Item, Converter={StaticResource NoteItemConverter}, ConverterParameter=tags}"/>
    
    <!-- Display both the name and the tags for the note -->
    <TextBlock Text="{Binding Item, Converter={StaticResource NoteItemConverter}}"/>
    

    请注意,“绑定项”是指列表中的每个单独元素。根据您的数据源的定义方式,您需要根据需要对其进行修改。

    【讨论】:

    • 我应该把什么作为绑定路径?你能给我一些演示代码吗?
    猜你喜欢
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多