如何在XAML ListBox中使用多重绑定? [英] How can I get multibinding to work in XAML ListBox?

查看:63
本文介绍了如何在XAML ListBox中使用多重绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面显示了列表框中3个"MultiTest.Model.Customers"(应该显示的每个记录一个).

The following shows me 3x "MultiTest.Model.Customers" in the ListBox (one for each record it should display).

我需要更改什么才能使其输出字段的内容?

What do I need to change in order for it to output the contents of the fields instead?

<Window.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate" >
            <Setter.Value>
                <MultiBinding StringFormat="{}{1}, {0} ">
                    <Binding Path="FirstName" />
                    <Binding Path="LastName"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox x:Name="theCustomers"/>
</Grid>

使用ADO.NET实体框架在代码背后进行绑定:

binding in code-behind with ADO.NET Entity Framework:

MainEntities db = new MainEntities();
var customers = from c in db.CustomersSet
                select c;
theCustomers.ItemsSource = customers;

答案:

谢谢,史蒂夫,这是我在Window.Resources格式中的回答:

ANSWER:

Thanks, Steve, here is your answer in my Window.Resources format:

<Window.Resources>
    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="ContentTemplate" >
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock>
                            <TextBlock.Text>
                                <MultiBinding  StringFormat="{}{1}, {0} ({2})">
                                    <Binding Path="FirstName"/>
                                    <Binding Path="LastName"/>
                                    <Binding Path="ID"/>
                                </MultiBinding>
                            </TextBlock.Text>
                        </TextBlock>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <ListBox x:Name="theCustomers"/>
</Grid>

推荐答案

如果您特别想使用MultiBinding,则应该可以将DataTemplate与StringFormat一起使用.

If you particularly want to use MultiBinding you should be able to use a DataTemplate with StringFormat.. something like:

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding  StringFormat="{}{1}, {0}">
                        <Binding Path="FirstName"/>
                        <Binding Path="LastName"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
       </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

或者对于更复杂的事情,您可以使用ValueConverter(或多重绑定变体).

Or for something more complicated you could use a ValueConverter (or the multiple binding variant).

这篇关于如何在XAML ListBox中使用多重绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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