绑定到ItemsControl中的CurrentItem

thr*_*rag 3 data-binding wpf itemscontrol collectionviewsource currentitem

下面的XAML基本上是尝试创建一个Buttons 列表(从当前集合中Name对象的属性呈现).ViewsDataContext

当我单击按钮时,CurrentItem属性CollectionViewSource应该更改,关联View应该显示在内容呈现器中.

好.如果我点击ListBox下面的XAML,它可以完全按照要求工作.

但是,如果单击UniformGrid(由项目控件创建)中的按钮,CurrentItem则不会更新该属性.

如何CurrentItemItemsControl?中选择项目时更新?

谢谢

<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>
Run Code Online (Sandbox Code Playgroud)

小智 11

你的按钮什么也没做.通常,您的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>
Run Code Online (Sandbox Code Playgroud)

ItemsControl的结合模式,让每个为MyModel模型得到一个按钮.按钮文本绑定到属性Name.button命令绑定到ViewModel中的Select属性.按下按钮时,它会调用ICommand,在MyModel实例中发送按钮绑定的内容.

请注意,在a中使用ViewModel UserControl是一种代码味道. UserControls应该对用户显示为所有其他控件 - 它们应该具有绑定到用户的ViewModel的可绑定公共属性,而不是你的.然后绑定到中的这些属性的值UserControl.对于此示例,您将在您的上定义ItemsSource属性UserControl,并且ItemsControl将直接绑定到此属性而不是ViewModel.