WPF: access controls from usercontrol
I have a usercontrol whit a RichTextBox control and a TreeView, and I want to bind a c开发者_JS百科ommands like Copy, Cut, Paste etc which are in Window not in usercontrol but I dont know how to access Elements from usercontrol? Any ideas?
You have to add a namespace to your XAML file and add your usercontrol with the given namespace prefix
as such:
xlmns:myuserctrl = "assemblyreference here"
Make sure you give your usercontrol a name which you can access in your code-behind.
You should then be able to access your controls from that variable inside your code-behind. If not, create some properties in the usercontrol class referencing the controls inside the user control.
There's no straightforward way of accessing child elements of user controls in XAML. However you can get creative and implement a custom type descriptor that exposes control fields as properties, which you can then bind to from the elements outside of the user control.
精彩评论