我正在使用PRISM的wpf應用程序。這是我的第一個WPF應用程序,我發現自己處於無法前進的狀態。我的場景是這樣的,我有一個組列表,我與一個列表框綁定的組,我們可以稱它爲父列表框,每個組對象都有一個與之關聯的用戶列表,它嵌套在父列表框中。這兩種綁定工作正常,你可以在這裏看到:WPF:如何將命令應用於嵌套列表框的上下文菜單
我面臨兩個問題。
1.我可以分別選擇組和單個用戶分組,但它們不同步,意思是如果我選擇一個用戶,那麼包含此用戶的組未被選中。我試過IsSynchronizedWithCurrentItem =「True」,但似乎沒有工作。
我非常感謝,如果有人能指出我在正確的方向如何實現這一目標,或者如果有任何其他方式旁邊使用列表框中的列表框。
2.我與父母的ListBox相關的上下文菜單,我能夠通過菜單成功綁定的命令,但我有麻煩結合嵌套ListBox的上下文菜單命令,這裏是我的代碼
<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>
我想知道是否有人可以在這裏指出我正確的方向。
在此先感謝。
感謝您的快速反應雷切爾,加上風格觸發,似乎做的伎倆,但我仍然無法綁定到上下文菜單命令用你上面提供的代碼。 – Saqib
@ user781466 PlacementTarget將返回任何具有ContextMenu的對象,因此請確保該元素的'DataContext'上存在'AddContestCommand'。您可能需要做一些冒險的事情來獲得正確的'DataContext',比如將該元素的'Tag'屬性綁定到具有所需命令的'DataContext'。我建議使用像[Snoop](http://snoopwpf.codeplex.com/)這樣的工具來找出確切的'DataContext'是什麼,你綁定到 – Rachel
@ user781466其實我只是注意到你的組'ListBox '當您的參賽者ContextMenu項目綁定到'ElementName = contestantManager'時,命名爲'lstOfGroups'。嘗試首先將'ElementName'改爲'lstOfGroups'。 – Rachel