1

私はPRISMを使用してwpfアプリケーションに取り組んでいます。これは私の初めてのWPFアプリケーションであり、先に進むことができない状況に陥っています。

私のシナリオはこれです、私はリストボックスでバインドしているグループのリストを持っています、あなたはそれを親リストボックスと呼ぶことができます、各グループオブジェクトはそれに関連付けられたユーザーのリストを持っていますそして私はそのリストをネストされている別のリストボックスでバインドしています親のリストボックス。ここに表示されているように、両方のバインディングは正常に機能しています。

ここに画像の説明を入力してください

私は2つの問題に直面しています。

1.グループとグループ内の個々のユーザーの両方を個別に選択できますが、同期していません。つまり、ユーザーを選択すると、このユーザーを含むグループが選択されません。IsSynchronizedWithCurrentItem = "True"を試しましたが、機能していないようです。

誰かがこれを達成する方法を正しい方向に指摘してくれるか、またはリストボックス内でリストボックスを使用する以外に他の方法があるかどうかを教えていただければ幸いです。

2.親のリストボックスに関連付けられたコンテキストメニューがあり、コマンドをメニューに正常にバインドできますが、ネストされたリストボックスのコンテキストメニューにコマンドをバインドするのに問題があります。これが私のコードです

<ListBox x:Name="lstOfGroups" 
             ItemsSource="{Binding CurrentContest.Groups}"
             SelectedItem="{Binding SelectedGroup}"
             ItemTemplate="{StaticResource GroupTemplate}"
             Style="{StaticResource ListBoxStyle1}"
             ItemContainerStyle="{StaticResource ListBoxItemStyle1}"
             Background="Transparent" SelectionMode="Single"
             IsSynchronizedWithCurrentItem="True"
             Height="400">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Horizontal" Margin="5" Width="1200"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>

                <ListBox.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Add Contestant" Command="{Binding AddGroupCommand}"/>
                        <MenuItem Header="Edit Contestant" Command="{Binding EditGroupCommand}"/>
                        <MenuItem Header="Delete Contestant" Command="{Binding DeleteGroupCommand}"/>
                    </ContextMenu>
                </ListBox.ContextMenu>
            </ListBox>

<DataTemplate x:Key="GroupTemplate" >
        <Border x:Name="spPubItemBorder" Margin="3" BorderBrush="Black" BorderThickness="1" CornerRadius="10" Background="Honeydew">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <StackPanel Orientation="Horizontal">
                    <TextBlock Name="tbGroupName" Grid.Column="0" Style="{StaticResource ItemTextBox}">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}Group Name: {0}">
                            <Binding Path="Name" />
                        </MultiBinding>
                    </TextBlock.Text>
                    </TextBlock>
                    <TextBlock Name="tbGroupAmount" Grid.Column="1" Style="{StaticResource ItemTextBox}">
                    <TextBlock.Text>
                        <MultiBinding StringFormat="{}Group Amount: {0}">
                            <Binding Path="Amount" />
                        </MultiBinding>
                    </TextBlock.Text>
                    </TextBlock>
                </StackPanel>
                <!--<ItemsControl ItemsSource="{Binding ContestantList}"
                              AlternationCount="2" ItemTemplate="{StaticResource ContestantTemplate}">

                </ItemsControl>-->
                <ListBox x:Name="lstOfContestant" Grid.Row="1"
                     ItemsSource="{Binding ContestantList}"
                     SelectedItem="{Binding SelectedContestant, ElementName=lstOfGroups}"
                     ItemTemplate="{StaticResource ContestantTemplate}"
                     Style="{StaticResource ListBoxStyleForContestant}"
                     ItemContainerStyle="{StaticResource ListBoxItemStyleForContestant}"
                     Background="Transparent" SelectionMode="Single"
                     Height="Auto">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel Orientation="Horizontal" Margin="5" Width="375"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>

                    <ListBox.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Add Contestant" Command="{Binding Path=DataContext.AddContestantCommand,ElementName=contestantManager}"/>
                            <MenuItem Header="Edit Contestant" Command="{Binding Path=DataContext.EditContestantCommand,ElementName=contestantManager}"/>
                            <MenuItem Header="Delete Contestant" Command="{Binding Path=DataContext.DeleteContestantCommand,ElementName=contestantManager}"/>
                        </ContextMenu>
                    </ListBox.ContextMenu>
                </ListBox>

            </Grid>
        </Border>
    </DataTemplate>

ここでも誰かが私を正しい方向に向けることができるかどうか疑問に思いました。

前もって感謝します。

4

1 に答える 1

1

#1IsSelectedの場合、グループListBoxのプロパティをtrueに設定します。IsKeyboardFocusWithin

<Style x:Key="GroupListBoxItemStyle" TargetType="ListBoxItem">
  <Style.Triggers>
    <Trigger Property="IsKeyboardFocusWithin" Value="True">
      <Setter Property="IsSelected" Value="True" />
    </Trigger>
  </Style.Triggers>
</Style>

ListBoxItemこれにより、キーボードフォーカスが内のどこかにある場合、グループが選択済みとして設定されます。ListBoxItem

ElementName#2に関しては、おそらくバインディングで使用しているために、間違ったアイテムを取得しているように聞こえますが、名前は複数のアイテムに設定されています。バインディングを使用して自分自身RelativeSourceを見つけてから、にバインドしてみてくださいContextMenuPlacementTarget.DataContext

<MenuItem Header="Add Contestant"
          Command="{Binding PlacementTarget.DataContext.AddContestantCommand, 
              RelativeSource={RelativeSource FindAncestor, 
              AncestorType={x:Type ContextMenu}}}" />
于 2012-06-12T16:18:28.077 に答える