c# - Simple string property won't seem to bind in XAML GUI -


i have page want display string property in label. code, nothing appear in label.

this .xaml

<page x:class="myproject.pageone"           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"         title="pageone"           name="pageone>     <grid>      <grid.columndefinitions>             <columndefinition width="*"/>     </grid.columndefinitions>     </grid>  <label grid.column="0" content="{binding elementname=pageone, path=astr}" fontweight="normal" fontsize="43" horizontalalignment="left" margin="0,00,0,0" verticalalignment="center" foreground="white"/> </grid>  </page> 

and .cs code

    public partial class pageone: page, ipageinterface     {        public string astr{get;set;}        public pageone()             {                 initializecomponent();              }        public void start()          {             astr = "test";          }      } 

the thing wrong code posted, in terms of problem describe, have not implemented way property change notifications occur. because astr property not set new value until after label content has been set, without way receive notification, framework has no way know needs update label content.

in wpf 2 main ways typically done (indeed, afaik 2 supported ways) create dependencyproperty instances, or implement inotifypropertychanged. either work fine.

here example of how code should inotifypropertychanged implemented:

public partial class pageone : page, ipageinterface, inotifypropertychanged {     private string _astr;     public string astr     {         { return _astr; }         set { _astr = value; onpropertychanged(); }     }      public page1()     {         initializecomponent();     }      public void start()     {         astr = "test";     }      private void onpropertychanged([callermembername] string propertyname = null)     {         propertychangedeventhandler handler = propertychanged;          if (handler != null)         {             handler(this, new propertychangedeventargs(propertyname));         }     }      public event propertychangedeventhandler propertychanged; } 

implementing interface involves couple of simple steps:

  1. declare event named propertychanged
  2. any time property changed, raise event, passing name of property that's changing.

note this, can't use auto-implemented properties. need implement each property yourself, backing field, , call method raise property.

.net offers convenient [callermembername] attribute, show here. in setter method astr property, after setting backing field's value, call method without parameters, , runtime automatically fills in correct property name you.


now, code posted has other problems well, in xaml. first won't compile because left out " character, , because you've got </grid> closing tag.

one other possible problem, though it's not possible know sure since missing full context of how display page object, text's color white. if you're putting label instance on white background, of course won't able see text, if set correctly.


note commenter franck has suggested should set datacontext. truth is, given code posted not necessary, , doing wouldn't fix problem having.

but if fix underlying notification issue, suggestions alternative way can achieve binding. setting datacontext object containing property (here, pageone class), when binding can specify property name alone, without having include elementname @ all, , without having use path= property name. may find technique more convenient, @ least of time.


in future, please take time provide a good, minimal, complete code example reliably reproduces problem. more answer way, , ensure answer can be.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -