c# - Silverlight: Difficulty with binding to visiblity -
i have 2 elements: listbox , "this list empty" message. i'd bind visibility list box's itemssource
being empty. however, i'm not sure how this:
<textblock text="no favorite searches yet. add searching, clicking 'add favorites'" padding="10,0" verticalalignment="center" visibility="{binding path=favoritefilters.isempty, converter={staticresource visibilityconverter}}" /> <listbox itemssource="favoritefilters" x:name="favoritefilterslist" visibility="{binding path=favoritefilters.isempty, converter={staticresource visibilityconverter}}"> <listbox.itemtemplate> <datatemplate> <my:favoritefilterlink /> </datatemplate> </listbox.itemtemplate> </listbox>
they both visible, although itemssource
empty. also, i'm not sure how invert condition listbox
.
the visibility converter:
public class visibilityconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value bool?) { if (string.isnullorempty((string)parameter)) { return (value bool?).value ? visibility.visible : visibility.collapsed; } else { return (value bool?).value ? visibility.collapsed : visibility.visible; } } throw new argumentexception(); } public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { throw new notimplementedexception(); } }
you might better off writing 2 converters, 1 listbox other textblock. logic of each simpler , wouldn't need pass parameter correct result.
while might not elegant single converter solution far more maintainable.
if want pass parameter need use converterparameter
.
there's example here i'm not 100% sure it'll meet requirements. simplified xaml syntax is:
<textblock visibility="{binding favoritefilters.isempty, converter={staticresource visibilityconverter}, converterparameter=false}"/> <listbox visibility="{binding favoritefilters.isempty, converter={staticresource visibilityconverter}, converterparameter=true}"/>
then in converter (simplified):
bool show = (bool)value; bool visible = (bool)parameter; if (visible) { return show ? visibility.visible : visibility.collapsed; } else { return show ? visibility.collapsed : visibility.visible; }
Comments
Post a Comment