我有一个ControlTemplate,其中包含设备的状态。在这种状态下,可以显示设备程序,包括三个步骤。动画索引属性的计数通常为1到3。该设备还可以执行三个独立于程序的操作。但是,程序应具有更高的优先级。操作1和操作2不能同时发生。 Operation3应该具有最低优先级。当操作或程序均未激活时,控件将保持为空。
使用数据触发器,根据状态更改画布的可见性。 我只想允许同时触发一个触发器。目前,我需要很多代码来折叠不可见的部分。我还依赖于检查条件的顺序:在代码中从上到下。哪个样式不好(可以通过更多代码避免)
<ControlTemplate>
<Viewbox Width="30" Height="24" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Top">
<Canvas Background="{TemplateBinding Background}" Width="30.6667" Height="26.6667">
<Canvas x:Name="deviceProgramIdle"Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepA" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepB" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepC" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation1" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation2" Height="24" Width="30" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation3" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
</Canvas>
</Viewbox>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding DeviceViewModel.IsOperation1Active, Mode=OneWay}" Value="true">
<Setter TargetName="deviceOperation1" Property="Visibility" Value="Visible"/>
<Setter TargetName="deviceProgramIdle" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepA" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepB" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepC" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding DeviceViewModel.IsOperation2Active, Mode=OneWay}" Value="true">
<Setter TargetName="deviceOperation2" Property="Visibility" Value="Visible"/>
<Setter TargetName="deviceProgramIdle" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepA" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepB" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepC" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DeviceViewModel.IsOperation1Active, Mode=OneWay}" Value="true"/>
<Condition Binding="{Binding DeviceViewModel.IsOperation2Active, Mode=OneWay}" Value="false"/>
<Condition Binding="{Binding DeviceViewModel.IsOperation3Active, Mode=OneWay}" Value="false"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="deviceOperation3" Property="Visibility" Value="Visible"/>
<Setter TargetName="deviceProgramIdle" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepA" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepB" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepC" Property="Visibility" Value="Collapsed"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="1">
<Setter TargetName="deviceOperation1" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation2" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation3" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepA" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="2">
<Setter TargetName="deviceOperation1" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation2" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation3" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepB" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="3">
<Setter TargetName="deviceOperation1" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation2" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceOperation3" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="deviceProgramStepC" Property="Visibility" Value="Visible"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
答案 0 :(得分:0)
如建议的@P.Manthe
默认为所有Canvas设置Visibility="Collapsed"
,而仅Property="Visibility" Value="Visible"
设置需要可见的画布
<ControlTemplate>
<Viewbox Width="30" Height="24" Stretch="Uniform" HorizontalAlignment="Right" VerticalAlignment="Top">
<Canvas Background="{TemplateBinding Background}" Width="30.6667" Height="26.6667">
<Canvas x:Name="deviceProgramIdle"Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepA" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepB" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceProgramStepC" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation1" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation2" Height="24" Width="30" Visibility="Collapsed">
...
</Canvas>
<Canvas x:Name="deviceOperation3" Width="30.6667" Height="26.6667" Visibility="Collapsed">
...
</Canvas>
</Canvas>
</Viewbox>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding DeviceViewModel.IsOperation1Active, Mode=OneWay}" Value="true">
<Setter TargetName="deviceOperation1" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding DeviceViewModel.IsOperation2Active, Mode=OneWay}" Value="true">
<Setter TargetName="deviceOperation2" Property="Visibility" Value="Visible"/>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DeviceViewModel.IsOperation1Active, Mode=OneWay}" Value="true"/>
<Condition Binding="{Binding DeviceViewModel.IsOperation2Active, Mode=OneWay}" Value="false"/>
<Condition Binding="{Binding DeviceViewModel.IsOperation3Active, Mode=OneWay}" Value="false"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="deviceOperation3" Property="Visibility" Value="Visible"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="1">
<Setter TargetName="deviceProgramStepA" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="2">
<Setter TargetName="deviceProgramStepB" Property="Visibility" Value="Visible"/>
</DataTrigger>
<DataTrigger Binding="{Binding AnimationIndex, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type devic:DeviceProgram}}, Mode=OneWay}" Value="3">
<Setter TargetName="deviceProgramStepC" Property="Visibility" Value="Visible"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>