Xaml - Be careful of binding with ContextMenu

来源:互联网 发布:知网期刊查重数据库 编辑:程序博客网 时间:2024/05/17 17:55

My context menu was binding its Tag to a viewmodels' list item, but wen the item changed, the Tag doesn't refresh.

<phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Grid x:Name="gridEvent" Background="Transparent" Tap="gridEvent_Tap" Tag="{Binding}" Margin="12,0,12,12">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Image Grid.Column="0" Width="70" Height="70" Stretch="Uniform" Source="{Binding Converter={StaticResource eventToStatusIcon}}"/>
                            <Grid Grid.Column="1">
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding StartTimeDisplay}"/>
                                <TextBlock Grid.Row="1" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding Subject}"/>
                                <TextBlock Grid.Row="2" Style="{StaticResource PhoneTextNormalStyle}" Text="{Binding DialString}"/>
                            </Grid>


                            <toolkit:ContextMenuService.ContextMenu>
                                <toolkit:ContextMenu>
                                    <toolkit:MenuItem x:Name="menuDetails"  Tag="{Binding }" Click="menuDetails_Click"
                                        Header="{Binding Source={StaticResource LocalizedStrings}, Path=LocalizedResources.Conference_Details}"/>
                                </toolkit:ContextMenu>
                            </toolkit:ContextMenuService.ContextMenu>
                        </Grid>
                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>


The reason probably is ContextMenu isn't in the visual tree and the binding only works the 1st time when context menu initialized. I have to bind the Tag to the item's Key which is never changed, then always get the item by key when use it.


0 0
原创粉丝点击