【问题标题】:Can you add an extra item to a data bound ItemsControl in XAML?您可以在 XAML 中向数据绑定 ItemsControl 添加额外的项目吗?
【发布时间】:2008-12-19 18:52:42
【问题描述】:

我有一个ItemsControl,它是绑定到decimals 列表的数据。我需要向ItemsControl 添加一个额外的控件(手动指定数字的选项)。有没有办法在 XAML 中做到这一点?我知道我可以在后面的代码中手动添加项目,但我试图更好地理解 WPF,并想看看是否有一种声明性的方式来做到这一点。

请注意,修改我绑定到的列表以使其包含额外按钮(可能通过更改为 strings 的列表而不是 decimals)不是一个好的选择,因为我想附加对最后一个按钮的命令。

另外,在ItemsControl 之后添加一个额外的按钮也不是一个好的选择,因为我的控件使用UniformGrid,我希望我的额外控件在同一个网格中。

这是我的 XAML:

<ItemsControl ItemsSource="{Binding PossibleAmounts}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Name="ButtonsGrid">
            </UniformGrid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Button>
                <TextBlock Text="{Binding StringFormat='\{0:C\}'}"/>
            </Button>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

基本上,我想在 UniformGrid 中再添加一个按钮。

【问题讨论】:

    标签: wpf data-binding xaml


    【解决方案1】:

    您可以为此目的使用CompositeCollection 类,它将多个集合或单个项目组合为 ItemsControl 的 ItemsSource。

    在 MSDN 文章中有一个很好的例子,或者这里是另一个:

    <Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:sys="clr-namespace:System;assembly=mscorlib"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Grid.Resources>
            <x:Array x:Key="intData" Type="{x:Type sys:Int32}">
                <sys:Int32>1</sys:Int32>
                <sys:Int32>2</sys:Int32>
                <sys:Int32>3</sys:Int32>
            </x:Array>
            <x:Array x:Key="stringData" Type="{x:Type sys:String}">
                <sys:String>Do</sys:String>
                <sys:String>Re</sys:String>
                <sys:String>Mi</sys:String>
            </x:Array>
        </Grid.Resources>
        <ListBox>
            <ListBox.ItemsSource>
                <CompositeCollection>
                    <CollectionContainer Collection="{StaticResource intData}"/>
                    <CollectionContainer Collection="{StaticResource stringData}"/>
                    <ListBoxItem>One more item!</ListBoxItem>
                    <ListBoxItem>Two more items!</ListBoxItem>
                </CompositeCollection>
            </ListBox.ItemsSource>
        </ListBox>
    </Grid>
    

    【讨论】:

    • 太棒了!没想到这样的东西竟然在WPF中作为标准功能实现了!
    • WindowsRT 有替代方案吗?我在 Win8 应用 XAML 中看不到 CompositeCollection,所以我认为对象本身不可用...
    【解决方案2】:

    不过,CompositeCollection 的问题在于它没有继承父元素 DataContext,因此您将无法编写:

    <CollectionContainer Collection={Binding ...}" />
    

    在它里面 - 或者更确切地说它会让你,但你不会从中得到任何东西。由于原始帖子要求 - {Binding PossibleAmounts} - 而不是绑定到 StaticResource - 这并不是真正的解决方案。

    见:

    http://blogs.msdn.com/nickkramer/archive/2006/08/18/705116.aspx

    http://www.vistax64.com/avalon/90-compositecollections-collectioncontainer-binding-issue.html

    http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bd1a78c1-67ef-4d1e-9cbe-70bbe8eb8b44/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多