Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

How To Disable ContextMenu in TextBox in a DataTemplate

$
0
0
I have a ListBox which binds to a bunch of items, and the listbox uses a DataTemplate to determine how each item is rendered

<Window x:Class="DataTemplateExample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <DataTemplate x:Key="listBoxDataTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Age}" ></TextBlock>
                <TextBox Background="Transparent" Text="{Binding Name}" ContextMenu="{x:Null}"></TextBox>
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <ListBox ItemTemplate="{StaticResource listBoxDataTemplate}" ItemsSource="{Binding Strings}">
            </ListBox>
            <TextBox ContextMenu="{x:Null}">Some Cool Text</TextBox>
        </StackPanel>
    </Grid>
</Window>

Above is a simple reproduction.

Note that in the TextBox that is in the datatemplate I set the ContextMenu to null, yet when you right click it at runtime the menu still pops up.

The TextBox at the bottom (not in a datatemplate) reacts as I would expect them both to.

Any ideas?

I'll put together a XAMLPad friendly version too.

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>