How to bind Close command to a button

前端 未结 7 1239
时光说笑
时光说笑 2020-11-30 19:09

The easiest way is to implement ButtonClick event handler and invoke Window.Close() method, but how doing this through a Command bindi

相关标签:
7条回答
  • 2020-11-30 19:49

    Actually, it is possible without C# code. The key is to use interactions:

    <Button Content="Close">
      <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
          <ei:CallMethodAction TargetObject="{Binding ElementName=window}" MethodName="Close"/>
        </i:EventTrigger>
      </i:Interaction.Triggers>
    </Button>
    

    In order for this to work, just set the x:Name of your window to "window", and add these two namespaces:

    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    

    This requires that you add the Expression Blend SDK DLL to your project, specifically Microsoft.Expression.Interactions.

    In case you don't have Blend, the SDK can be downloaded here.

    0 讨论(0)
提交回复
热议问题