c# - Access objects in UserControl from MainWindow in WPF -


i have simple xaml, how can change text property of textblock in usercontrol1 mainwindow?

<window x:class="refactorxaml.mainwindow"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         xmlns:refactorxaml="clr-namespace:refactorxaml"         title="mainwindow" height="350" width="525">     <grid>         <grid.rowdefinitions>             <rowdefinition height="100"></rowdefinition>             <rowdefinition height="100"></rowdefinition>         </grid.rowdefinitions>         <grid>             <refactorxaml:usercontrol1></refactorxaml:usercontrol1>         </grid>         <grid grid.row="1">             <button>change text</button>         </grid>     </grid> </window>  <usercontrol x:class="refactorxaml.usercontrol1"          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"           d:designheight="300" d:designwidth="300">          <textblock x:name="mytextblock">stackoverflow</textblock> </usercontrol> 

controls in wpf meant kind of “black boxes”. shouldn’t (and don’t) care what’s going on inside. instead, if want allow communication, define public interface users of control can communicate with.

in wpf, public interface made using dependency properties. can use normal (clr) properties many things well, want allow things data binding, , need dependency properties. definining dependency properties bit more complicated definining normal properties, require stuff done additionally. in visual studio, there propdp snippet though, helps adding boilerplate code in few simple steps.

so, user control, have single string property, want expose outside. in code-behind user control, define property dependency property:

public partial class exampleusercontrol : usercontrol {     public static readonly dependencyproperty sometextproperty =         dependencyproperty.register("sometext", typeof(string), typeof(exampleusercontrol), new propertymetadata("default value"));      public string sometext     {         { return (string)getvalue(sometextproperty); }         set { setvalue(sometextproperty, value); }     }      // same old stuff     public exampleusercontrol()     {         initializecomponent();     } } 

that’s need declare property , make available users of component. need add functionality. there isn’t logic involved, want delegate value other component’s property (textblocks’s text property), can use data binding. that’s work cases:

<usercontrol x:class="somenamespace.exampleusercontrol" …         datacontext="{binding relativesource={relativesource self}}">     <stackpanel>         <textblock text="{binding sometext}" />     </stackpanel> </usercontrol> 

to make property accessible data binding, set data context self (i.e. component itself) , can data bind own properties.

when using component then, can set sometext property of it:

<my:exampleusercontrol sometext="foo" /> 

and it’s dependency property, can use data binding value somewhere else (e.g. view model when using mvvm):

<my:exampleusercontrol sometext="{binding sometextproperty}" /> 

and if give control name, can access , sometext property in code-behind if need that:

<my:exampleusercontrol x:name="mycontrol" sometext="foo" /> <button click="changetext_click" content="change text" /> 
private void changetext_click (object sender, routedeventargs e) {     mycontrol.sometext = "bar"; } 

in end, have reusable component separated application used in. , application not need know implementation of component, , component not need know it’s going used.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

javascript - How to name a jQuery function to make a browser's back button work? -