I have a rectangle with its Visibility
property bound to the view-model. For (apparent) performance reasons, I'm setting the DataContext
in the page's Loaded
eve开发者_StackOverflow社区nt. This rectangle should be collapsed by default. Unfortunately, with this "late-context" pattern, the rectangle shows for a few fractions of a second.
Is there a no-code way to make it collapsed by default?
If no, I could simply set the property to Collapsed
and bind it in the Loaded event, but I have many such rectangles in my app.
Could I implement a ContentControl
that is collapsed until loaded? Is that second idea too far-fetched?
@Martin - have you tried setting the FallbackValue in your binding?
I don't have the VS at hand to check it, but I think that if your setup is like (sorry for 'errors', thats just a draft:
<rectangle visibility={Binding mydata.somthing.someVisibilityProperty} />
then, if binding at Loaded, your control may "flash" with the default Visibility.Visible value. Your binding fails at the first render, because there's no data bound yet. If so, then just setup the fallbackvalue:
<rectangle visibility={Binding mydata.somthing.someVisibilityProperty, FallbackValue=Collapsed} />
this will cause the binding to return "Visibility.Collapsed" whenever it fails to read from the source.
In case of
<Rectangle Visibility="{Binding TipRoundingHasError, Converter={StaticResource VisibilityConverter}}">
that you have presented in your second answer, it would basically look like:
<Rectangle Visibility="{Binding TipRoundingHasError, FallbackValue=DEFAULTVALUE, Converter={StaticResource VisibilityConverter}}">
but I dont recall now, whether yout Converter will be invoked on the FallbackValue or not. That means, that I cant tell you now, if you should substitute DEFAULTVALUE for "Collapsed" or rather for "False". But I think you will test&choose the correct one in an instant.
For more examples on Fallback, try looking at the BindingBase.FallbackValue - there's nice example with a custom binding class (yeah, not only converters may be custom:) )
I made an AppearingControl
who's implementation defies calling this "coding" since it's so simple:`
public class AppearingControl : ContentControl
{
public AppearingControl()
{
if( !System.ComponentModel.DesignerProperties.IsInDesignTool )
{
this.Visibility = System.Windows.Visibility.Collapsed;
this.Loaded += new RoutedEventHandler( AppearingControl_Loaded );
}
}
void AppearingControl_Loaded( object sender, RoutedEventArgs e )
{
this.Loaded -= new RoutedEventHandler( AppearingControl_Loaded );
this.ClearValue( AppearingControl.VisibilityProperty );
}
}
I can use the control this way:
<slim:AppearingControl HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<Rectangle Visibility="{Binding TipRoundingHasError, Converter={StaticResource VisibilityConverter}}">
<Rectangle.Fill>
<SolidColorBrush Color="#FFFF4040" Opacity="0.5"/>
</Rectangle.Fill>
</Rectangle>
</slim:AppearingControl>
Can someone confirm I didn't just reinvent the wheel, or worse, use a bazooka to kill a fly?
Thanks.
精彩评论