SilverLight:从基本文件调用Child Control事件

时间:2014-11-10 14:05:06

标签: silverlight

1)BaseFile

   文件包含按任意键时调用的KeyDown事件。

    private void MODULE_KeyDown(object sender, KeyEventArgs e)
    {
          //Here Need to Check Key press is (CTRL + ALT + S)
        if (Keyboard.Modifiers == (ModifierKeys.Alt | ModifierKeys.Control) && e.Key == Key.S)
        {
            var objMBox = new ITSUI.MBox("Do you want to save records?", "Confirm", MBoxButtons.YesNo);
            objMBox.Closed += delegate
            {
                if (objMBox.m_Return == MBoxReturn.Yes)
                {
                  **Here Need to call Detail Child Control OnOk Event**                
                }
            };
            objMBox.Show();
        }
    }

2)细节儿童控制

    public void OnOk(object sender, RoutedEventArgs e)
    {
          //Save Record Code Here
    }

如何从基础文件调用OnOk子控制事件?

1 个答案:

答案 0 :(得分:1)

如果您需要从代码隐藏中调用子控件的方法,则可以在视图中为控件设置名称,并使用其名称。

<UserControl class="MyView">
    ...
    <MyChildControl x:Name="RecordEditView"/>
    ...
</UserControl>

并在您的代码隐藏中:

public class MyView : UserControl {
  public MyView()
  {
    InitializeComponent();
    this.KeyDown += OnMyViewKeyDown;
  }

  private void OnMyViewKeyDown(object sender, KeyEventArgs e)
  {
    RecordEditView.SaveRecordData();
  }
}
public class MyChildControl : UserControl
{
    public void SaveRecordData()
    {
        ...
    }
}