3

以下のXAMLは、基本的にsのリストを作成しようとしています(現在のコレクション内のオブジェクトのプロパティButtonからレンダリングされます。NameViewsDataContext

ボタンをクリックすると、のCurrentItemプロパティがCollectionViewSource変更され、関連Viewするものがコンテンツプレゼンターに表示されます。

わかった。下の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があります。

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のインスタンスが送信されます。

UserControl内でViewModelsを使用することは、コードの臭いであることに注意してください。 UserControlsは、他のすべてのコントロールと同じようにユーザーに表示される必要があります。これらのコントロールには、ユーザーのViewModelではなく、ユーザーのViewModelにバインドされるバインド可能なパブリックプロパティが必要です。次に、内のこれらのプロパティの値にバインドしますUserControl。この例では、にItemsSourceプロパティを定義しUserControlViewModelを直接バインドするItemsControlのではなく、このプロパティにバインドします。

于 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 に答える