下面的XAML基本上是尝试创建一个Button列表(从当前DataContext中Views集合中对象的Name属性呈现。
当我单击一个按钮时,CollectionViewSource的CurrentItem属性应该会改变,相关的View应该会显示在内容呈现器中。
好的。如果我在下面的XAML中单击ListBox,它将完全按照预期工作。
但是,如果单击UniformGrid (由items控件创建)中的按钮,则不会更新CurrentItem属性。
如何让CurrentItem在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>发布于 2009-12-21 21:10:57
你的按钮什么也不做。通常,您的ViewModel会有一个名为Select (或类似的内容)的ICommand,按钮将绑定到该the上
Command="{Binding Select, ElementName="root"}"
然后将实例传递给想要选择的ICommand
CommandParameter="{Binding}"
它看起来像这样(c#/XAML类似于伪代码):
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绑定到Models,因此每个MyModel in Models都有一个按钮。按钮文本绑定到属性名称。该按钮命令绑定到ViewModel中的选择属性。当按钮被按下时,它调用ICommand,并将该按钮绑定到的MyModel实例发送进来。
请注意,在UserControl中使用ViewModels是一种代码嗅觉。UserControl应该向用户显示为所有其他控件--它们应该具有绑定到用户的ViewModel的公共属性,而不是您的。然后在UserControl中绑定到这些属性的值。对于此示例,您将在UserControl上定义一个ItemsSource属性,并且ItemsControl将绑定到此属性,而不是直接绑定到ViewModel。
发布于 2009-12-21 21:14:04
我认为您必须在代码隐藏中手动完成此操作。
XAML
<Button Click="ViewListButton_Click">
<TextBlock Text="{Binding Path=Name}" Name="txtButtonLabel" VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="Black"/>
</Button>代码隐藏
private void ViewListButton_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
ICollectionView view = CollectionViewSource.GetDefaultView(ViewList.ItemsSource);
view.MoveCurrentTo(button.DataContext);
}如果您正在使用MVVM模式和/或不想使用代码隐藏,则可以按照Will的建议将按钮的Command绑定到ViewModel中的命令
https://stackoverflow.com/questions/1939907
复制相似问题