开发者

WPF: control losing its binding

开发者 https://www.devze.com 2023-01-27 06:26 出处:网络
I have a MenuItem of a context menu of a Grid and its IsEnabled property is bound to MyProperty. It works fine until I actually click on the event handler of the MenuItem, which has no code in it. Aft

I have a MenuItem of a context menu of a Grid and its IsEnabled property is bound to MyProperty. It works fine until I actually click on the event handler of the MenuItem, which has no code in it. After I have clicked on the MenuItem the dataBinding no longer works.

I can tell cos' my breakpoint in the getter of MyProperty does not get hit.

any ideas why?

Here is the code:

    <my:DataGrid.ContextMenu >
        <ContextMenu Name="restoreCntextMenu" DataContext="{Binding PlacementTarget.SelectedItem, RelativeSource={RelativeSource Self}}">
            <MenuItem Header="Data Service"  Click="dataServiceToolStripMenuItem_DropDownOpening">
                <MenuItem Header="Start" Click="startDataToolStripMenuItem_Click" IsEnabled="{Binding MyProperty}" /> 
            </MenuItem>
        </ContextMenu>
    </my:DataG开发者_运维百科rid.ContextMenu>


Context menus are not part of the visual tree like everything else is, so your bindings won't work like they do everywhere else.

Instead you need to bind to the placement target. See the accepted answer from this question.


My guess would be that something is changing the datacontext. I would setup a button or something and then click your menu then the button and in the button check the menu's datacontext to ensure it's what you think it should be.

0

精彩评论

暂无评论...
验证码 换一张
取 消