Reputation: 8594
I'm buiding a WPF custom control I'm calling a ResponseTimer. This basically consists of a DispatcherTimer and a ProgressBar. It has properties called TimeElapsed and TimeoutPeriod that are TimeSpans. In the user interface of my program, events occur periodically. There is a time period within which the user can resopnd to the event. If the interval expires, the program takes action on its own.
I want to use this control in two places. One will be on an item that appears in a ListBox, the other at the bottom of a window. For the copes in the ListBox, when the time period has expired, I want to hide the ProgressBar, while I don't want to hide the control at the bottom of the window.
To get this functionality, I've defined two bool DependcyProperties called HideIfExpired and IsExpired. If HideIfExpired is true, then the ProgressBar will be hidden if IsExpired is true. Simple.
I want to use triggers in the default content template for the control in Generic.xaml. I'm not sure how to write the triggers, though.
Here's the XAML for the control:
<Style TargetType="{x:Type local:ResponseTimer}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:ResponseTimer}">
<StackPanel Visibility="{TemplateBinding Visibility}">
<ProgressBar Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
FlowDirection="{TemplateBinding FlowDirection}"
Foreground="{TemplateBinding Foreground}"
Height="{TemplateBinding Height}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
IsEnabled="{TemplateBinding IsEnabled}"
IsTabStop="False"
Margin="{TemplateBinding Margin}"
MaxHeight="{TemplateBinding MaxHeight}"
MaxWidth="{TemplateBinding MaxWidth}"
MinHeight="{TemplateBinding MinHeight}"
Minimum="0"
MinWidth="{TemplateBinding MinWidth}"
Name="PART_ProgressBar"
Opacity="{TemplateBinding Opacity}"
OpacityMask="{TemplateBinding OpacityMask}"
Orientation="{TemplateBinding Orientation}"
Padding="{TemplateBinding Padding}"
Panel.ZIndex="{TemplateBinding Panel.ZIndex}"
RenderSize="{TemplateBinding RenderSize}"
RenderTransform="{TemplateBinding RenderTransform}"
RenderTransformOrigin="{TemplateBinding RenderTransformOrigin}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
Style="{TemplateBinding Style}"
Tag="{TemplateBinding Tag}"
ToolTip="{TemplateBinding ToolTip}"
UseLayoutRounding="{TemplateBinding UseLayoutRounding}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Visibility="{Binding Path=IsExpired, Converter={StaticResource InvertedBoolToVisibility}, RelativeSource={RelativeSource TemplatedParent}}"
Width="{TemplateBinding Width}" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'm not sure any more why the StackPanel is in there, it may be a remnant of a previous version of the control. I might replace it with a Border, just to have a way to hide the whole thing.
Anyway, how do I write my triggers?
Upvotes: 0
Views: 1905
Reputation: 3492
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsExpired" Value="True" />
<Condition Property="HideIfExpired" Value="True" />
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Visibility" Value="Collapsed" />
</MultiTrigger.Setters>
</MultiTrigger>
</ControlTemplate.Triggers>
Upvotes: 1