WPF绑定ItemsControl中字符串值的列表

时间:2016-01-29 08:24:44

标签: c# wpf xaml binding

我有以下Model类

 ResultViewModel
 public string word {get;set;}
 public List<string> meanings {get;set;}

有2个属性。如何将意义列表字符串绑定到堆栈面板?

我的数据就像

字=&#34;&#34; ,含义=&#34; a1&#34;,&#34; a2&#34;,&#34; a3&#34;

字=&#34; B&#34; ,含义=&#34; b1&#34;,&#34; b2&#34;,&#34; b3&#34; ....

字=&#34; C&#34; ,含义=&#34; c1&#34;,&#34; c2&#34;,&#34; c3&#34; ....

我有跟随xaml,这将帮助我绑定&#34; a&#34; &#34; B&#34; &#34; C&#34;等等但现在我想在其底部显示含义

    <ItemsControl Name="lviewLookupResult"  Background="#363636">
        <ItemsControl.ItemTemplate>
            <DataTemplate>                  
                    <StackPanel Orientation="Horizontal" Margin="5 5 5 0">                          
                        <Label Margin="5 0" Padding="0"  Content="{Binding word}" />
                    </StackPanel>
                    <StackPanel Orientation="Vertical" Margin="5 5 5 0">                            

                        ??????HOW CAN I SHOW Meanings HERE???????????


                    </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我使用下面的代码绑定模型,绑定在运行时很好

  List<ResultViewModel> view_model = new List<ResultViewModel>();           
   lviewLookupResult.ItemsSource = view_model;

4 个答案:

答案 0 :(得分:4)

使用另一个ItemsControl,如下所示:

   <ItemsControl Name="lviewLookupResult"  Background="#363636">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <StackPanel Orientation="Horizontal" Margin="5 5 5 0">
                        <Label Margin="5 0" Padding="0"  Content="{Binding word}" />
                    </StackPanel>
                    <ItemsControl Margin="5 5 5 0" ItemsSource="{Binding meanings}">
                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding}"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

答案 1 :(得分:2)

请尝试更新此XAML。

<ItemsControl Name="lviewLookupResult"  Background="#363636">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal" Margin="5 5 5 0">
                    <Label Margin="5 0" Padding="0"  Content="{Binding word}" />
                    <ListView ItemsSource="{Binding meanings}">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal">
                                    <Label Content="{Binding}"/>
                                </StackPanel>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </StackPanel>

            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

答案 2 :(得分:1)

我建议创建一个名为MeaningsCollection的类并覆盖ToString方法。

public class MeaningsCollection
{
    public List<string> meanings {get;set;}

    public override string ToString()
    {
        return "the collection in your preferred format";
    }
}

public class ResultViewModel 
{
     public string word {get;set;}
     public MeaningsCollection Meanings {get;set;}
}

在你的XAML中应该是这样的:

<ItemsControl Name="lviewLookupResult"  Background="#363636">
        <ItemsControl.ItemTemplate>
            <DataTemplate>                  
                    <StackPanel Orientation="Horizontal" Margin="5 5 5 0">                          
                        <Label Margin="5 0" Padding="0"  Content="{Binding word}" />
                        <Label Margin="5 0" Padding="0"  Content="{Binding Meanings}" />
                    </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

答案 3 :(得分:1)

使用ItemsControl并设置ItemsPanel属性。

<ItemsControl Name="lviewLookupResult"  Background="#363636">
    <ItemsControl.ItemTemplate>
        <DataTemplate>                  
                <StackPanel Orientation="Horizontal" Margin="5 5 5 0">                          
                    <Label Margin="5 0" Padding="0"  Content="{Binding word}" />
                </StackPanel>

                <ItemsControl ItemsSource="{Binding meanings}">                            
                      <ItemsControl.ItemsPanel>
                          <ItemsPanelTemplate>
                               <StackPanel Orientation="Vertical" Margin="5 5 5 0" />
                          </ItemsPanelTemplate>
                      </ItemsControl.ItemsPanel>
                </ItemsControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>