views:

45

answers:

2

.I am creating a user control that stacks three WPF month calendars (Master, Slave1, and Slave2) vertically. I want the second and third calendars to be hidden until the host window is large enough to show the entire month--no partial calendars. I have implemented the feature be trapping the SizeChanged event in an event handler in code-behind:

/// <summary>
/// Shows only complete calendars--partially visible calendars are hidden.
/// </summary>
private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
    // Increasing window height
    var newHeight = e.NewSize.Height;
    if (newHeight > 500)
    {
        SlaveCalendar1.Visibility = Visibility.Visible;
        SlaveCalendar2.Visibility = Visibility.Visible;
    }
    else if (newHeight > 332)
    {
        SlaveCalendar1.Visibility = Visibility.Visible;
        SlaveCalendar2.Visibility = Visibility.Hidden;
    }
    else
    {
        SlaveCalendar1.Visibility = Visibility.Hidden;
        SlaveCalendar2.Visibility = Visibility.Hidden;
    }
}

It works fine, but I would rather implement the feature in XAML, and I am not sure how to do it. Any suggestions? I'm not looking for someone else to write this for me--just point me in the right direction. Thanks for your help.

A: 

You should be able to do this with xaml templates and triggers. Probably event triggers for the OnSizeChanged event. I believe

Here are some resources to get you pointed in the right direction.

http://msdn.microsoft.com/en-us/library/cc294856%28Expression.30%29.aspx http://www.wpfdude.com/articles/Triggers.aspx http://www.geektieguy.com/2008/01/05/wpf-controltemplate-trigger-tip/

nportelli
The links are interesting, but the answer is incorrect. Triggers can't solve the problem in pure XAML because they can't do numeric comparisons. See Aviad P.'s answer for a good solution to this.
Ray Burns
Fine do to the opposition this may be more along what you are looking for. http://andyonwpf.blogspot.com/2007/02/how-small-can-you-go-part-ii.html
nportelli
+1  A: 

One idea is to create a new panel type, derived from Panel (or maybe StackPanel) which is called for example IntegralClippingPanel. In that panel's overrides of MeasureOverride and ArrangeOverride make sure to only display child elements if they fit in their entirety inside the available space.

Aviad P.