Edd*_*eyo 15 data-binding wpf xaml
我有一个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>
Run Code Online (Sandbox Code Playgroud)
基本上,我想在UniformGrid中再添一个按钮.
Rob*_*nee 20
您可以将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>
Run Code Online (Sandbox Code Playgroud)
小智 17
但是,CompositeCollection的问题在于它不会继承父元素DataContext,因此您将无法编写:
<CollectionContainer Collection={Binding ...}" />
Run Code Online (Sandbox Code Playgroud)
在它内部 - 或者更确切地说它会让你,但你不会得到任何东西.由于原始帖子需要 - {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/
| 归档时间: |
|
| 查看次数: |
8267 次 |
| 最近记录: |