Context menu DataBinding not working on ListView binding

171 views Asked by At

In my WPF window there is Button with context menu on that. I define Context menu Template as Static Resource. In Context Menu ControlTemplate there is a ListView. I have bind this ListView ItemTemplate via DataBinding.

Now I need to show this Context Menu on Button mouse hover. When I right click on button then the context menu is show also my binding is working, able to update items in ListView control.

But when I try to show ContextMenu on Button PreviewMouseMove event then binding is not working.

Code used to open the context menu:

btn_1.ContextMenu.IsOpen = true;

My Listview control is empty.

Please suggest what to do to fill ListView items on PreviewMouseMove event.

In WPF window

public mainWindow()
{
    MyViewModel oModel = new MyViewModel();
    InitializeComponent();
    this.DataContext = oModel.prd1;
}

in XAML Button control

<Button Name="brd_Product1" Content={Binding ProductName} PreviewMouseMove="brd_PreviewMouseMove">
                <Button.ContextMenu>
                    <ContextMenu Template="{StaticResource tmpl_Offer}" 
                                 ContextMenuService.VerticalOffset="-25" ContextMenuService.HorizontalOffset="50">
                    </ContextMenu>
                </Button.ContextMenu>
            </Button>

and in MyViewModel class

public cOffer prd1{ get; set; }
public MyViewModel()
        {
            cOffer prd1 = new cOffer()
            {
                ProductName = "Product1",
                listFeatures = new List<string> {
                                "Features1",
                                "Features2",
                                "Features3"},
            };
        }

Control Template define

<ControlTemplate x:Key="tmpl_Offer"
                         TargetType="{x:Type ContextMenu}">
            <Border CornerRadius="5" Background="#3A3A3A">
                <ListView Name="lview_FeatureList"
                                  ItemsSource="{Binding listFeatures}"ScrollViewer.CanContentScroll="False" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" IsHitTestVisible="False">
                            <ListView.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock VerticalAlignment="Center"
                                                   Text="{Binding}"
                                                   TextTrimming="CharacterEllipsis"></TextBlock>
                                    </StackPanel>
                                </DataTemplate>
                            </ListView.ItemTemplate>
                        </ListView>
            </Border>
        </ControlTemplate>

public class cOffer
        {
            public string ProductName { get; set; }
            public List<string> listFeatures { get; set; }
        }
0

There are 0 answers