WPF Button.Contextmenu

Button์•ˆ์— ContextMenu๊ฐ€ ์žˆ๊ณ  ์ขŒํด๋ฆญ์ด๋˜ ์šฐํด๋ฆญ์ด๋˜ Contextmenu๋ฅผ ์ž‘๋™ํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋Š”๋ฐ

  1. ์šฐํด๋ฆญ(Button) โ†’ ์ขŒ ๋˜๋Š” ์šฐํด๋ฆญ (Contextmenu)๋ฅผ ํ•ด์•ผ
  2. ์ขŒํด๋ฆญ(Button) โ†’ ์ขŒ ๋˜๋Š” ์šฐํด๋ฆญ (Contextmenu)์ด ์ž‘๋™์ด ๋˜๋„ค์š”

1๋ฒˆ ๊ณผ์ •์„ ํ•˜์ง€ ์•Š์œผ๋ฉด 2๋ฒˆ์—์„œ Contextmenuํด๋ฆญ์‹œ ๋ฐ˜์‘์ด ์—†๋„ค์š”.

<Button Content="Option" HorizontalAlignment="Center" VerticalAlignment="Center" 
                x:Name="OptionBtn"
                
                Cursor="Hand" Width="163" Grid.Row="1" 
                DataContext="{Binding DataViewModel}" >
            <Button.ContextMenu>
                <ContextMenu x:Name="OptionContextmenu" >
                    <MenuItem Header="Edit" Command="{Binding Edit_Command}" />                            
                    <MenuItem Header="Add"   Command="{Binding Add_Command}" />                                      
                    <MenuItem Header="List" Command="{Binding List_Command}" />                        
                </ContextMenu>
            </Button.ContextMenu>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <i1:ChangePropertyAction TargetName="OptionContextmenu"
                                             PropertyName="IsOpen"
                                             Value="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Button>

๋‹ค์Œ๊ณผ ๊ฐ™์€๋ฐ 1๋ฒˆ ํ–‰๋™์„ ๋ฌด์กฐ๊ฑดํ•ด์•ผ์ง€ 2๋ฒˆ์ด ์ž‘๋™ํ•˜๋„ค์š” ใ… ใ… 

WPF ์งˆ๋ฌธ์ด์–ด์„œ ์œˆ๋„์šฐ ๋ฐ์Šคํฌํƒ‘ ์นดํ…Œ๊ณ ๋ฆฌ๋กœ ๊ธ€์„ ์ด๋™ํ•ด๋“œ๋ ธ์Šต๋‹ˆ๋‹ค.

์ข‹์•„์š” 1

์•„ํ•˜ ๋„ต ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์Šคํ† ๋ฆฌ๋ณด๋“œ๋ฅผ ํ™œ์šฉํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค.

<Button Grid.Row="2" Content="Option" HorizontalAlignment="Center" VerticalAlignment="Center" 
                x:Name="OptionBtn"
                Cursor="Hand" Width="163">
            <Button.Triggers>
                <EventTrigger RoutedEvent="Button.Click">
                    <EventTrigger.Actions>
                        <BeginStoryboard>
                            <Storyboard>
                                <BooleanAnimationUsingKeyFrames Storyboard.TargetName="OptionContextmenu"
                                                                Storyboard.TargetProperty="IsOpen">
                                    <DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="True"/>
                                </BooleanAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger.Actions>
                </EventTrigger>
            </Button.Triggers>

            <Button.ContextMenu>
                <ContextMenu x:Name="OptionContextmenu">
                    <MenuItem Header="Edit"/>
                    <MenuItem Header="Add"/>
                    <MenuItem Header="List"/>
                </ContextMenu>
            </Button.ContextMenu>
</Button>
์ข‹์•„์š” 2

์•— ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ์ ์šฉํ•ด๋ณผ๊ป˜์š”!