绑定到ItemsControl中的CurrentItem [英] Binding to CurrentItem in a ItemsControl
问题描述
下面的XAML基本上是试图列出 Button
s(从 Name
在当前 DataContext
中的视图
集合。
The XAML below is basically trying to make a list of Button
s (rendered from the Name
property of objects in the Views
collection in the current DataContext
.
当我点击一个按钮时, CurrentItem
属性 CollectionViewSource
应该更改,并且相关联的查看
应该显示在内容主持人中。
When I click on a button the CurrentItem
property of CollectionViewSource
should change and the associated View
should be displayed in a content presenter.
好的,如果我点击 ListBox
但是,如果我点击 UniformGrid
(创建)中的按钮通过项目控件) CurrentItem
属性未更新。
But, If I click a button in the UniformGrid
(created by the items control) the CurrentItem
property is not updated.
如何获取 CurrentItem
在 ItemsControl
中选择项目时进行更新?
How do I get the CurrentItem
to be updated when an item is selected in the ItemsControl
?
谢谢
<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>
推荐答案
您的按钮什么都不做。通常你的ViewModel会有一个名为Select(或类似的东西)的ICommand,这个Button将被绑定到
Your button does nothing. Usually your ViewModel would have an ICommand called Select (or something similar) that the Button would be bound against
Command ={Binding Select,ElementName =root}
,你会将实例传递给要选择的ICommand
and you'd pass the instance to the ICommand that you'd like to select
CommandParameter ={Binding}
它看起来像这个(c#/ XAML就像伪代码):
It would look something like this (c#/XAML like pseudocode):
public class MyModel { public string Name {get;set;} }
public class MyViewModel
{
public ObservableCollection<MyModel> Models {get;set;}
public ICommand Select {get;set;}
/* configure Models and Select etc */
}
<UserControl DataContext="{StaticResource MyViewModelInstance}" x:Name="root">
<ItemsControl ItemsSource="{Binding Models}">
<ItemsControl.ItemTemplate>
<ItemsPanelTemplate>
<Button Text="{Binding Name}"
Command="{Binding Select, ElementName="root"}"
CommandParameter="{Binding}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
ItemsControl 绑定到模型,所以模型中的每个 MyModel 获取一个按钮。按钮文本被绑定到属性名称。按钮命令绑定到ViewModel中的选择属性。当按下按钮时,它会调用ICommand,发送在 MyModel 的实例中,按钮被绑定。
The ItemsControl binds to Models, so each MyModel in Models gets a button. The button text is bound to the property Name. The button command is bound to the Select property in the ViewModel. When the button is pressed, it calls the ICommand, sending in the instance of MyModel that the button is bound against.
请注意在 UserControl
中使用ViewModels是 代码气味 。 UserControl
应该以用户身份显示为所有其他控件 - 他们应该具有可绑定的公共属性,这些属性绑定到用户的 ViewModel ,而不是您的。然后,您可以绑定到 UserControl
中的这些属性的值。对于此示例,您将在 UserControl
和 ItemsControl
ItemsSource 属性>将直接绑定到此属性而不是 ViewModel 。
Please do note that using ViewModels within a UserControl
is a code smell. UserControl
s should appear to users as all other controls--they should have bindable public properties which are bound to the user's ViewModel, not yours. You then bind to the values of these properties within the UserControl
. For this example, you would have an ItemsSource property defined on your UserControl
, and the ItemsControl
would bind to this property rather than a ViewModel directly.
这篇关于绑定到ItemsControl中的CurrentItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!