Listbox binding in Silverlight -
i have listbox on silverlight page, datacontext of page set instance -- testquestions, please see code below. listbox uses datatemplate , itemsource 'answers' property of page's datacontext, works fine until try bind 'showanswer', property on page's datacontext within datatemplate. no matter tried won't pick property's value.
thank help.
xaml:
<listbox itemssource="{binding path=answers, mode=twoway}"> <listbox.itemtemplate> <datatemplate> <radiobutton ischecked="{binding path=correct, mode=twoway}" > <grid> <stackpanel visibility="{binding path=showanswer}"> <rectangle fill="blue" visibility="{binding path=correct}" /> </stackpanel> <textblock text="{binding path=answertext, mode=twoway}" textwrapping="wrap" /> </grid> </radiobutton> </datatemplate> </listbox.itemtemplate>
c#:
public class answer { private bool correct = false; public bool correct { { return correct; } set { correct = value; notifypropertychanged("correct"); } } private string answertext = false; public string answertext { { return answertext; } set { answertext = value; notifypropertychanged("answertext"); } } } public class testquestions { private observablecollection<answer> answers = new observablecollection<answer>(); public observablecollection<answer> answers { { return answers; } set { if (answers != value) { answers = value; notifypropertychanged("answers"); } } } private bool showanswer = false; public bool showanswer { { return showanswer; } set { showanswer = value; notifypropertychanged("showanswer"); } }
}
it looks me you're trying bind uielement.visibility boolean value. change 'showanswer' property boolean visibility property , should set.
private visibility showanswer = visibility.collapsed; public visibility showanswer { { return showanswer; } set { showanswer = value; notifypropertychanged("showanswer"); } }
edit:
if trying bind property on datacontext of parent control, this:
name usercontrol
example:
<usercontrol x:class="mvvmlightproto.mainpage" x:name="mainprotopage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" height="700" width="1200">
in above example, usercontrol has name of "mainprotopage", in xaml, this:
<stackpanel visibility="{binding datacontext.showanswer, elementname=mainprotopage}">
Comments
Post a Comment