绑定字典< string,List< customObject>>到WPF中的ListBox [英] Binding Dictionary<string, List<customObject>> to ListBox in WPF

查看:31
本文介绍了绑定字典< string,List< customObject>>到WPF中的ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Dictionary< string,List< WritingMachine>> ,其中键是写入服务器的计算机的主机名,值是WritingMachine对象的列表(如下所示)):

I have a Dictionary<string, List<WritingMachine>> where the key is the hostname of a machine that writes to a server, and the value is an list of WritingMachine objects (shown below):

public class WritingMachine
{
    public string HostName { get; set; }
    public string Program { get; set; }
    public string Login { get; set; }
    public string Server { get; set; }
}

我正在尝试将字典绑定到WPF ListBox;显示主机名(字典的键),以及与该键关联的 WritingMachine 列表的摘要.

I'm trying to bind the dictionary to a WPF ListBox; displaying the Hostname (the dictionary's key), and a summary of the WritingMachine list that's associated with that key.

我无法访问列表中各个 WritingMachine 元素的属性.

I'm having trouble accessing the properties of the individual WritingMachine elements within the list.

到目前为止,这是我的XAML:

Here's my XAML so far:

<ListBox x:Name="MachineListBox" Margin="10" Grid.Column="0" FontFamily="Consolas" ItemsSource="{Binding Path=MachineDictionary}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical" Margin="5">
                <ContentPresenter Content="{Binding Key}" Margin="0,0,4,0"/>
                <ItemsControl ItemsSource="{Binding Value}" Margin="10,0,0,0">
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Vertical" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

这将导致一个如下所示的列表框:

This results in a listbox that looks like this:

显然这是错误的,因为 WritingMachine 列表元素只是返回默认的 ToString .通过覆盖 WritingMachine ToString ,我可以(或多或少)获得想要的效果:

Obviously this is wrong because the WritingMachine list elements are just returning the default ToString. By overriding WritingMachine's ToString I can get the effect I want (more or less):

但这是一种垃圾的方式...我希望能够使用 Content ="{绑定Value.Server}"或类似的文件.

But this a crap way of doing it... I want to be able to access the individual element properties and arrange them in controls in my ListBox.ItemTemplate using Content="{Binding Value.Server}" or similar.

有什么想法吗?

推荐答案

对于ItemsControl,您需要设置一个ItemTemplate:

For your ItemsControl you need to set an ItemTemplate:

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock>Server: </TextBlock>
                    <TextBlock Text="{Binding Server}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

这会让您入门.

这篇关于绑定字典&lt; string,List&lt; customObject&gt;&gt;到WPF中的ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆