开发者

WPF Close Window Closes Source Window. Why?

开发者 https://www.devze.com 2023-03-16 10:27 出处:网络
The problem that I am having is a little hard to describe, so please hear it out. I\'m simply opening one window from another and then trying to close the second one. If I use Command of the InputBin

The problem that I am having is a little hard to describe, so please hear it out.

I'm simply opening one window from another and then trying to close the second one. If I use Command of the InputBindings of the second one, the second one closes fine. If I call the close directly it closes both the first and second window. I expect code will help is this scenario.

WPF: Window1View (key part)

<Grid>
  <Button Content="Button" Command="{Binding RptKeyDownCommand}" />
</Grid>

Window1ViewModel: (shortened for listing)

using GalaSoft.MvvmLight.Command;
var _runCommand = new RelayCommand(() => Run(), () => CanRun());

    public void Run()
    {
        var v = new Window2();
        var vm = new Window2ViewModel();
        vm.RequestClose += v.Close;
        v.DataContext = vm;
        v.ShowDialog();
    }

    public event Action RequestClose;

    var _closeCommand = new RelayCommand(() => Close(), () => CanClose());
    public void Close()
    {
        if 开发者_高级运维(RequestClose != null)
            RequestClose();
    }

WPF: Window2View

<Window.InputBindings>
    <KeyBinding Key="Escape" Command="{Binding CloseCommand}" />
</Window.InputBindings>
    <TextBox Text="Hello">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="PreviewKeyDown">
                <cmd:EventToCommand 
                     Command="{Binding Close2Command, Mode=OneWay}"
                     PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>

Window2ViewModel: (has the same Close Command plus EventToCommand end point)

var _close2Command = new RelayCommand<KeyEventArgs>(p => Close2(p), p => CanClose2(p));

    public void Close2(KeyEventArgs e)
    {
        if (e.Key == Key.Escape)
            Close();               <- Here closes both Window1View and Window2View?
    }


See this answer on your other thread for a solution.


From Window2ViewModel you should Call RequestClose not Close.

Here is the code for Window2ViewModel

RelayCommand _close2Command;
public ICommand Close2Command
{
    get
    {
        if (_close2Command == null)
        {
            _close2Command = new RelayCommand(param => CloseEx(), param => CanClose());
        }
        return _close2Command;
    }
}

public virtual void CloseEx()
{
    Close();
}

public event Action RequestClose;
public virtual void Close()
{
    if (RequestClose != null)
    {
        *RequestClose();*
    }
}
public virtual bool CanClose()
{
    return true;
}

Also Window1ViewModel should have code as :

using GalaSoft.MvvmLight.Command;           
var _runCommand = new RelayCommand(() => Run(), () => CanRun()); 
var vm;

public void Run()               
{                   
    var v = new Window2();                   
    vm = new Window2ViewModel();                   
    vm.RequestClose += CloseV2;                   
    v.DataContext = vm;                   
    v.ShowDialog();               
}                          

public event Action RequestClose;                          
var _closeCommand = new RelayCommand(() => Close(), () => CanClose());    

public void CloseV2()               
{                   
    vm.Close();              
}

public void Close()               
{                   
    if (RequestClose != null)                       
    RequestClose();               
}

Try to understand your code. Note in you code you are binding event of both V1.RequestClose to V2.RequestClose to same method Close. In my case I have them separate and V2.RequestClose will always call vm.Close.

hope this helps.

0

精彩评论

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