簡體   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