Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
933 views
in Technique[技术] by (71.8m points)

wpf - Can you add an extra item to a data bound ItemsControl in XAML?

I have an ItemsControl that is data bound to a list of decimals. I need to add one extra control to the ItemsControl (an option to specify the number manually). Is there a way to do this in XAML? I know I can manually add the item in the code behind, but I'm trying to understand WPF a little better and want to see if there is a declarative way to do it.

Note that modifying the list I'm binding to so that it includes the extra button (possibly by changing to a list of strings instead of decimals) isn't a good alternative because I want to attach a command to that last button.

Also, adding an extra button after the ItemsControl isn't a good option either, because my control uses a UniformGrid and I want my extra control in that same grid.

Here is my 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>

Basically, I want one more button in the UniformGrid.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use the CompositeCollection class for this purpose, it combines multiple collections or individual items as the ItemsSource for an ItemsControl.

There's a good example in the MSDN article, or here's another one:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...