c# - WPF popup with textbox? -
c# - WPF popup with textbox? -
i trying create command displays search results user types in textbox. this, have textbox , popup shows when user types (just google search box). so,
<grid> <textbox name="userentry" /> <popup /> </grid>
now when user starts typing textbox want popup show , remain open until user focusses on other ui command or if text entered empty. unable accomplish , wondering if there alternate improve ways of doing in wpf. regards
xaml :
<window> <grid> <grid.rowdefinitions> <rowdefinition /> <rowdefinition /> </grid.rowdefinitions> <button x:name="btn" content="open search window" height="30" width="150" click="btn_click"/> <popup x:name="popup" placementtarget="{binding elementname=btn}" placement="bottom" width="200" height="100" margin="0,20,0,0"> <border borderbrush="black" borderthickness="2" background="aliceblue"> <textbox x:name="txtbox" verticalalignment="center" margin="15,0,15,0"/> </border> </popup> <textbox x:name="focustarger" text="focus me !" grid.row="1" horizontalalignment="center" verticalalignment="center" width="200" textalignment="center" fontsize="16"/> </grid> </window>
cs :
public partial class mainwindow : window { public mainwindow() { initializecomponent(); gotfocus += mainwindow_gotfocus; } void mainwindow_gotfocus(object sender, routedeventargs e) { frameworkelement element = (frameworkelement)e.originalsource; if (txtbox == element || popup == element || element.parent == popup) return; popup.isopen = !string.isnullorempty(txtbox.text); } private void btn_click(object sender, routedeventargs e) { popup.isopen = true; } }
c# wpf
Comments
Post a Comment