绑定到ItemsControl中的CurrentItem
下面的XAML基本上试图制作一个
Button
s列表(从当前DataContext
中Views
集合中的对象的Name
属性渲染。
当我点击一个按钮时,CollectionViewSource
的CurrentItem
属性应该改变,相关的View
应该在内容演示者中显示。
好。如果我点击下面XAML中的ListBox
,它可以完全按照要求工作。
但是,如果单击UniformGrid
(由项目控件创建)中的按钮,则不会更新CurrentItem
属性。
如何在ItemsControl
中选择项目时如何更新CurrentItem
?
谢谢
<UserControl x:Class="Pos.Features.Reservation.ReservationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:product="clr-namespace:Pos.Features.ProductBrowser"
xmlns:activity="clr-namespace:Pos.Features.ActivityBrowser"
xmlns:addbysku="clr-namespace:Pos.Features.AddBySku"
xmlns:client="clr-namespace:Pos.Features.ClientBrowser"
xmlns:notes="clr-namespace:Pos.Features.Notes"
xmlns:controls="clr-namespace:Pos.Views"
xmlns:res="clr-namespace:Pos.Core;assembly=Pos.Core"
Height="300" Width="300">
<UserControl.Resources>
<DataTemplate DataType="{x:Type product:ProductBrowserViewModel}">
<product:ProductBrowserView/>
</DataTemplate>
<DataTemplate DataType="{x:Type activity:ActivityBrowserViewModel}">
<activity:ActivityBrowserView/>
</DataTemplate>
<CollectionViewSource x:Name="x" x:Key="ViewsCollection" Source="{Binding Views}" />
</UserControl.Resources>
<StackPanel>
<ListBox Name="ListBoxMenu" Grid.Column="0" Margin="5" ItemsSource="{Binding Source={StaticResource ViewsCollection}}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" Padding="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ContentControl Grid.Column="1" Content="{Binding ElementName=ListBoxMenu, Path=SelectedItem}"/>
<ItemsControl Grid.Column="2" Name="ViewList" ItemsSource="{Binding Source={StaticResource ViewsCollection}}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Button>
<TextBlock Text="{Binding Path=Name}" Name="txtButtonLabel" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black"/>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" Columns="{Binding Views.Count}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<ContentControl Grid.Column="3" Content="{Binding Source={StaticResource ViewsCollection}, Path=CurrentItem}"/>
<Button Grid.Column="4" Click="Button_Click">dsadsd</Button>
</StackPanel>
</UserControl>
没有找到相关结果
已邀请:
2 个回复
骂狮淋唐便
然后你将实例传递给你想要选择的ICommand
它看起来像这样(c#/ XAML像伪代码):
ItemsControl绑定到Models,因此Models中的每个MyModel都会获得一个按钮。按钮文本绑定到属性Name。 button命令绑定到ViewModel中的Select属性。按下按钮时,它会调用ICommand,在MyModel实例中发送按钮绑定的内容。 请注意,在
中使用ViewModels是代码味道。
s应该作为所有其他控件显示给用户 - 它们应该具有绑定到用户的ViewModel的可绑定公共属性,而不是你的。然后绑定到
中这些属性的值。对于此示例,您将在
上定义ItemsSource属性,并且
将直接绑定到此属性而不是ViewModel。
外镶受继
代码隐藏
如果您正在使用MVVM模式和/或您不想使用代码隐藏,则可以将按钮的
绑定到ViewModel中的命令,如Will所建议的那样