繁体   English   中英

转换(字典 <string, List<string> &gt;)value).Values),这是IValueConverter数组显示在列表框中的集合

[英]Converting (Dictionary<string, List<string>>) value).Values)which is a collection to array by IValueConverter to show in a listbox

我的转换器如下-

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var fields = (((Dictionary<string, List<string>>) value).Values);

        string dogCsv = string.Join("", (object[]) fields.ToArray());
        string dogCsv1 = string.Join("", dogCsv.ToArray());
        Array ab = dogCsv1.ToArray();
        return ab;
    }

我的WPF绑定如下-

<ListBox x:Name="txt"  FontSize="20" Height="Auto" Width="Auto" MinHeight="100" MinWidth="100"
          ItemsSource="{Binding Obj.StudDetail,ElementName=window, Converter={StaticResource Converter}}">
    <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding}"  Background="Gray"/>
                <ListBox   Height="Auto" FontSize="20" MinHeight="100"
                           MinWidth="100" Width="Auto"
                           ItemsSource="{Binding Obj.StudDetail, 


ElementName=window, Converter={StaticResource Converter1}}"
                               HorizontalContentAlignment="Center"/>
                        </StackPanel>
                    </DataTemplate>
        </ListBox.ItemTemplate>
         </ListBox>

我已经使用datatemplate自定义UI。 我希望输出是我的字典的值。 请帮忙。 提前致谢。

以下是我的C#代码XAML.CS-

public partial class MainWindow : Window
{
    public Person Obj { get; set; }

    public MainWindow()
    {
        Obj = new Person();

        List<string> subjects1 = new List<string>();

        subjects1.Add("C++");
        subjects1.Add("C");
        subjects1.Add("C#");

        List<string> subjects2 = new List<string>();

        subjects2.Add("JAVA");
        subjects2.Add("JS");
        subjects2.Add("CSS");

        Obj.StudDetail.Add("Kushagra", subjects1);
        Obj.StudDetail.Add("Yash", subjects2);

        DataContext = this;
    }

    public class Person
    {
        private Dictionary<string, List<string>> _studDetail = new Dictionary<string, List<string>>();

        public Dictionary<string, List<string>> StudDetail
        {
            get { return _studDetail; }
            set { _studDetail = value; }
        }
    }
}

我还有一个转换器类,它返回键的集合。 它被命名为ValueConverter。

您甚至不需要转换器,就可以显示这样的数据

<ListBox x:Name="txt"  FontSize="20" Height="Auto" Width="Auto" MinHeight="100" MinWidth="100"
                 ItemsSource="{Binding Obj.StudDetail,ElementName=window}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding Path=Key}"  Background="Gray"/>
                        <ListBox   Height="Auto" FontSize="20" MinHeight="100"
                                   MinWidth="100" Width="Auto" ItemsSource="{Binding Path=Value}"
                                   HorizontalContentAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

我在这里所做的是将TextBlock绑定到Key,将嵌套列表框绑定到Value

尽管您实际上不需要使用值转换器(如其他答案所示),但它可能看起来如下图所示-使用您可能要使用的任何分隔符字符串代替空格。

请注意,转换器不会转换整个字典,而只会转换其单独的KeyValuePair条目,这些条目由ListBox的ItemTemplate中的{Binding Converter={StaticResource Converter}传递。

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var result = "";

    if (value is KeyValuePair<string, List<string>>)
    {
        var kvp = (KeyValuePair<string, List<string>>)value;

        result = kvp.Key + " " + string.Join(" ", kvp.Value);
    }

    return result;
}

您的XAML就是这样:

<ListBox ItemsSource="{Binding Obj.StudDetail, ElementName=window}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Converter={StaticResource Converter}}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

或者,分别绑定KeyValue属性,后一个绑定使用一个将List<string>转换为List<string>的转换器:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var values = value as IEnumerable<string>;

    return values != null ? string.Join(" ", values) : "";
}

使用此ItemTemplate:

<ListBox.ItemTemplate>
    <DataTemplate>
        <TextBlock>
            <Run Text="{Binding Key, Mode=OneWay}"/>
            <Run Text="{Binding Value, Converter={StaticResource Converter}, Mode=OneWay}"/>
        </TextBlock>
    </DataTemplate>
</ListBox.ItemTemplate>

或者这个:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Key}"/>
            <TextBlock Text=" "/>
            <TextBlock Text="{Binding Value, Converter={StaticResource Converter}}"/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM