c# - How to create command in usercontrol? -
i'm developing first application in wpf pattern mvc , have question.
i have created usercontrol type grid
made custom title bar. grid contains x button , want associate button command.
my grid in xaml:
<grid x:class="views.titlebarview" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" style="{dynamicresource titlestylegrid}" x:name="barview"> <label x:name="labelappname" style="{dynamicresource titlestylelabel}" content="title"/> <button x:name="bttnclose" style="{dynamicresource buttonstyleclosewindow}" command="{binding closecommand}"/> <button x:name="buttonminimize" style="{dynamicresource buttonstyleminimizewindow}" command="{binding minimizecommand}"/> </grid>
the c# view:
public partial class titlebarview : grid { public titlebarview() { initializecomponent(); titlebarviewmodel tvm = new titlebarviewmodel(); tvm.requestclose += (s, e) => this.close(); tvm.requestminimize+= (s, e) => this.minimize(); datacontext = tvm; } private void close() { window.getwindow(this).close(); } private void minimize() { application.current.mainwindow.windowstate = system.windows.windowstate.minimized; } }
the c# viewmodel:
public class titlebarviewmodel : viewmodelbase, irequestminimizeviewmodel, irequestcloseviewmodel { private relaycommand minimizecommand; protected relaycommand closecommand; public event eventhandler requestminimize; public event eventhandler requestclose; #region minimizecommand public icommand minimizecommand { { if (minimizecommand == null) { minimizecommand = new relaycommand(minimize); } return minimizecommand; } } private void minimize() { requestminimize(this, null); } #endregion #region closecommand public icommand closecommand { { if (closecommand == null) { closecommand = new relaycommand(close); } return closecommand; } } protected void close() { requestclose(this, null); } #endregion }
i saw it's not recommended set datacontext on usercontrol. , when this, can't change close command. example want when main windows calls command close calls application.current.shutdown();
instead of application.current.shutdown();
i know have wrong i'm confuse solve it. can explain me how create command usercontrol ? (or tell me i'm doing wrong)
thank
Comments
Post a Comment