3

下面的XAML 基本上是试图制作一个Buttons 列表(从当前.NameViewsDataContext

当我点击一个按钮时,应该改变的CurrentItem属性和相关的应该显示在内容演示器中。CollectionViewSourceView

好的。如果我在下面的 XAML 中单击,ListBox它会完全按照需要工作。

但是,如果我单击UniformGrid(由项目控件创建)中的按钮,则该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>
4

2 回答 2

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>

ItemsControl绑定到Models ,因此Models中的每个MyModel都有一个按钮。按钮文本绑定到属性Name。按钮命令绑定到 ViewModel 中的Select属性。当按钮被按下时,它调用 ICommand,发送按钮绑定的MyModel实例。

请注意,在 a 中使用 ViewModelUserControl是一种代码异味UserControls 应该像所有其他控件一样出现在用户面前——它们应该具有绑定到用户的ViewModel的可绑定公共属性,而不是您的。然后绑定到UserControl. 对于此示例,您将在上定义ItemsSource属性UserControl,并且ItemsControl将绑定到该属性而不是直接绑定到ViewModel

于 2009-12-21T13:10:56.940 回答
1

我认为您必须在代码隐藏中手动执行此操作。

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按照 Will 的建议将按钮绑定到 ViewModel 中的命令

于 2009-12-21T13:14:04.283 回答