绑定到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>
    
已邀请:
你的按钮什么也没做。通常,您的ViewModel会有一个名为Select(或类似名称)的ICommand,Button会被绑定
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,因此Models中的每个MyModel都会获得一个按钮。按钮文本绑定到属性Name。 button命令绑定到ViewModel中的Select属性。按下按钮时,它会调用ICommand,在MyModel实例中发送按钮绑定的内容。 请注意,在
UserControl
中使用ViewModels是代码味道。
UserControl
s应该作为所有其他控件显示给用户 - 它们应该具有绑定到用户的ViewModel的可绑定公共属性,而不是你的。然后绑定到
UserControl
中这些属性的值。对于此示例,您将在
UserControl
上定义ItemsSource属性,并且
ItemsControl
将直接绑定到此属性而不是ViewModel。     
我认为你必须在代码隐藏中手动完成它。 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模式和/或您不想使用代码隐藏,则可以将按钮的
Command
绑定到ViewModel中的命令,如Will所建议的那样     

要回复问题请先登录注册