换整个Template和换Template里某个属性(项目)

Bool?属性切换不同控件

方法1:换整个某个Custom的control的整个Template可以用DataTemplateDataTrigger,用在collection里(ListView/DataGrid),特点是在Template外面,换Template


例子:主要是换自定义的<Control>,用<DataTemplate.Triggers>换在DT里<Control>Template

View Code
<DataTemplate x:Key="SensorCheckRow_DataTemplate">
    <Control x:Name="SwitchControl">
      <Control.Template>
        <ControlTemplate TargetType="{x:Type Control}">
          <Rectangle HorizontalAlignment="Center" VerticalAlignment="Center"
                     Width="23"
                     Height="23"                        
                     Fill="{StaticResource Icon_Hover_Job}" />
        </ControlTemplate>
                
      </Control.Template>
    </Control>
    <DataTemplate.Triggers>
      <DataTrigger Binding="{Binding Path=IsSensorCheckPassed}" Value="True">
        <Setter TargetName="SwitchControl" Property="Template" Value="{StaticResource TickIcon_ControlTemplate}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding Path=IsSensorCheckPassed}" Value="False">
        <Setter TargetName="SwitchControl" Property="Template" Value="{StaticResource ErrorIcon_ControlTemplate}"/>
      </DataTrigger>
      <DataTrigger Binding="{Binding Path=IsSensorCheckPassed}" Value="{x:Null}">
        <Setter TargetName="SwitchControl" Property="Template" Value="{StaticResource Empty_ControlTemplate}"/>
      </DataTrigger>
    </DataTemplate.Triggers>
  </DataTemplate>

其各种模板的定义:注意x:Type要和上面DT里要换Template的Control类型一致

View Code
 1 <ControlTemplate x:Key="Empty_ControlTemplate" TargetType="{x:Type Control}">
 2     <Grid/>
 3   </ControlTemplate>
 4   
 5   <ControlTemplate x:Key="ErrorIcon_ControlTemplate" TargetType="{x:Type Control}">
 6     <Rectangle HorizontalAlignment="Center" VerticalAlignment="Center"
 7                Width="23"
 8                Height="23"                        
 9                Fill="{StaticResource Icon_CrossForDataGrid}" />
10   </ControlTemplate>
11   
12   <ControlTemplate x:Key="TickIcon_ControlTemplate" TargetType="{x:Type Control}">
13     <Rectangle HorizontalAlignment="Center" VerticalAlignment="Center"
14                Width="23"
15                Height="23"                        
16                Fill="{StaticResource Icon_TickForDataGrid}" />
17   </ControlTemplate>
18   
19   <ControlTemplate x:Key="ProgramButton_ControlTemplate" TargetType="{x:Type Control}">
20             <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
21               <Button Width="76"
22                             Height="26"
23                             FontSize="10pt"
24                             Command="{Binding Path=DataContext.ProgramToolCommand,RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
25                             CommandParameter="{Binding ToolName}"
26                             Content="Program"
27                             Style="{StaticResource General_Button_Style}" 
28                             BorderBrush="#FF2F2F2F"/> 
29           </Grid>
30  </ControlTemplate>

这样调用:

1  <DataGridTemplateColumn 
2               Width="*"
3               IsReadOnly="True" 
4               HeaderTemplate="{StaticResource SensorCheckHeader_DataTemplate}"
5               CellTemplate="{StaticResource SensorCheckRow_DataTemplate}"/>
View Code

 

方法2:把不同状态下的Contorl叠在一起写在一个不带x:TypeControlTemplate里(即不是通用应用这个模板,而是通过Key来匹配),ControlTemplate的每一个子Tag都可以用x:Name在trigger中再定位ControlTemplate的DataTrigeer来切换各个control的Visibility。

View Code
 1 <ControlTemplate x:Key="LoadingSpinner_ControlTemplate" >
 2               <Grid>
 3                <StackPanel x:Name="LoadingState" Orientation="Vertical">
 4                     <localUserControl:CircleLoadingAnimation  RenderTransformOrigin="0.5,0.5" 
 5                                                               Width="303.868" 
 6                                                               Height="150" 
 7                                                               Margin="0,-20,0,0">
 8                        <localUserControl:CircleLoadingAnimation.RenderTransform >
 9                            <TransformGroup>
10                                <ScaleTransform ScaleX="0.2" ScaleY="0.2"/>
11                            </TransformGroup>
12                        </localUserControl:CircleLoadingAnimation.RenderTransform>
13                    </localUserControl:CircleLoadingAnimation>
14                     <TextBlock TextWrapping="Wrap" Text="Starting Electro-Hydraulic Lock Check" Style="{DynamicResource Univers55_TextBlock_Style}" Margin="300,-95,300,0" />
15                 </StackPanel>
16                 
17                 <StackPanel x:Name="PassState" Orientation="Vertical" >
18                 <ContentControl x:Name="LoadingSpinnerPass"
19                                 Style="{StaticResource loadingSpinnerControlStyle}" 
20                                 RenderTransformOrigin="0.5,0.5" 
21                                                               Width="303.868" 
22                                                               Height="150" 
23                                                               Margin="0,30,0,0">
24                     <ContentControl.RenderTransform>
25                         <TransformGroup>
26                             <ScaleTransform ScaleX="0.2" ScaleY="0.2"/>
27                         </TransformGroup>
28                     </ContentControl.RenderTransform>
29                 </ContentControl>
30                 <TextBlock TextWrapping="Wrap" Text="Check Passed" Style="{DynamicResource Univers55_TextBlock_Style}" Margin="300,0"/>
31             </StackPanel>
32             
33                 <StackPanel x:Name="FailState" Orientation="Vertical" >
34                     <ContentControl 
35                                     x:Name="LoadingSpinnerFail"
36                                     Style="{StaticResource loadingSpinnerControlStyle}" 
37                                     RenderTransformOrigin="0.5,0.5" 
38                                     Width="303.868" 
39                                     Height="150" 
40                                     Margin="0,30,0,0">
41                         <ContentControl.RenderTransform>
42                             <TransformGroup>
43                                 <ScaleTransform ScaleX="0.2" ScaleY="0.2"/>
44                             </TransformGroup>
45                         </ContentControl.RenderTransform>
46                     </ContentControl>
47                     <TextBlock TextWrapping="Wrap" Text="Check Failed" Style="{DynamicResource Univers55_TextBlock_Style}" Margin="300,0"/>
48                 </StackPanel>
49             </Grid>
50                 <ControlTemplate.Triggers>
51                   <DataTrigger Binding="{Binding IsLockCheckStatus}" Value="{x:Null}">
52                     <Setter TargetName="LoadingState" Property="Visibility" Value="Visible"/>
53                     <Setter TargetName="PassState" Property="Visibility" Value="Collapsed"/>
54                     <Setter TargetName="FailState" Property="Visibility" Value="Collapsed"/>
55                   </DataTrigger>
56                   <DataTrigger Binding="{Binding IsLockCheckStatus}" Value="True">
57                     <Setter TargetName="PassState" Property="Visibility" Value="Visible"/>
58                     <Setter TargetName="LoadingState" Property="Visibility" Value="Collapsed"/>
59                     <Setter TargetName="FailState" Property="Visibility" Value="Collapsed"/>
60                   </DataTrigger>
61                   <DataTrigger Binding="{Binding IsLockCheckStatus}" Value="False">
62                     <Setter TargetName="FailState" Property="Visibility" Value="Visible"/>
63                     <Setter TargetName="LoadingState" Property="Visibility" Value="Collapsed"/>
64                     <Setter TargetName="PassState" Property="Visibility" Value="Collapsed"/>
65                   </DataTrigger>
66                 </ControlTemplate.Triggers>
67               </ControlTemplate>

 

注意:ControlTemplate的DataTrigger最常用的还是Change模板里某一个元素的属性

换ControlTemplate里某个元素的属性:一般用在同一个custom contro的某个元素的属性的改变,比变换buttoncommandparametercontent

用在单个控件,特点是在Template里面,换Template里面的设置

View Code
  <Style x:Key="AddToolOrUpdateToggleButton" TargetType="{x:Type ToggleButton}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ToggleButton}" >
                        <Button x:Name="AddToolOrUpdateButton"
                                Width="110"
                                Height="36"
                                HorizontalAlignment="Right"
                                Command="{Binding AddUpdateToolCommand}"
                                KeyboardNavigation.TabIndex="4"
                                Style="{StaticResource General_Button_Style}" 
                                BorderBrush="#FF2F2F2F" />
                        <ControlTemplate.Triggers>
                            <DataTrigger Binding="{Binding IsAddTool}" Value="True">
                                <Setter TargetName="AddToolOrUpdateButton" Property="Content" Value="Add Tool" />
                                <Setter TargetName="AddToolOrUpdateButton" Property="CommandParameter" Value="AddTool" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding IsAddTool}" Value="False">
                                <Setter TargetName="AddToolOrUpdateButton" Property="Content" Value="Update" />
                                <Setter TargetName="AddToolOrUpdateButton" Property="CommandParameter" Value="UpdateTool" />
                            </DataTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 

方法3:ContentControl当作一个custom control,内部切换ContentTemplate

利用了<ContentControl>可以是任何control的基类,创建我们自己的<ContentControl>与后台的VM下的Bool?bind在一起,切换<ContentControl>ContentTemplate属性DataTemplate类型的

首先Resource中定义三种DT资源(注意一定要写DataType="{x:Type vmodel:ProgramResultViewModel})。

x:Key="ProgramStatus_EqualTrue_View"
 1 <DataTemplate x:Key="ProgramStatus_EqualTrue_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
 2         <StackPanel x:Name="ProgramPassedPanel" Orientation="Vertical">
 3           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" Margin="0,20,0,0"><Run Text="This tool has been"/><LineBreak/><Run Text="successfully programmed."/></TextBlock>
 4 
 5           <Grid KeyboardNavigation.TabNavigation="None" 
 6                 Margin="0,20,0,0" 
 7                 HorizontalAlignment="Center" 
 8                 VerticalAlignment="Center">
 9             <Grid.ColumnDefinitions>
10               <ColumnDefinition Width="*" />
11               <ColumnDefinition Width="Auto" />
12             </Grid.ColumnDefinitions>
13             <Grid.RowDefinitions>
14               <RowDefinition Height="*" />
15               <RowDefinition Height="*" />
16               <RowDefinition Height="*" />
17               <RowDefinition Height="*" />
18               <RowDefinition Height="*" />
19             </Grid.RowDefinitions>
20 
21 
22             <TextBlock FontSize="16pt" Margin="0,2" Foreground="#FF414142"
23                        Style="{StaticResource Univers55_TextBlock_Style}" 
24                        Text="Serial Number"  VerticalAlignment="Center" HorizontalAlignment="Left" />
25             <TextBlock  Grid.Column="1"
26                         HorizontalAlignment="Left"
27                         FontSize="16pt" Foreground="#FF6D6E71"
28                         Style="{StaticResource Univers55_TextBlock_Style}"
29                         Text="{Binding CurrentTool.SerialNumber}" VerticalAlignment="Center" Margin="15,5,0,5" />
30 
31             <TextBlock FontSize="16pt" Grid.Row="1" Margin="0,2"
32                        Style="{StaticResource Univers55_TextBlock_Style}"
33                        Text="Tool Name"  VerticalAlignment="Center" HorizontalAlignment="Left" />
34             <TextBlock  Grid.Column="1" Grid.Row="1"
35                         HorizontalAlignment="Left"
36                         FontSize="16pt" Foreground="#FF6D6E71"
37                         Style="{StaticResource Univers55_TextBlock_Style}"
38                         Text="{Binding CurrentTool.ToolName}" VerticalAlignment="Center" Margin="15,5,0,5" />
39 
40             <TextBlock FontSize="16pt" Grid.Row="2" Margin="0,2"
41                        Style="{StaticResource Univers55_TextBlock_Style}"
42                        Text="Ball Count"  VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Light" />
43             <TextBlock  Grid.Column="1" Grid.Row="2"
44                         HorizontalAlignment="Left"
45                         FontSize="16pt" Foreground="#FF6D6E71"
46                         Style="{StaticResource Univers55_TextBlock_Style}"
47                         Text="{Binding CurrentTool.BallCount}" VerticalAlignment="Center" Margin="15,5,0,5" />
48 
49             <TextBlock FontSize="16pt" Grid.Row="3" Margin="0,2"
50                        Style="{StaticResource Univers55_TextBlock_Style}"
51                        Text="Sleep Duration"  VerticalAlignment="Center" HorizontalAlignment="Left" />
52             <TextBlock  Grid.Column="1" Grid.Row="3"
53                         HorizontalAlignment="Left"
54                         FontSize="16pt" Foreground="#FF6D6E71"
55                         Style="{StaticResource Univers55_TextBlock_Style}"
56                         Text="{Binding CurrentTool.SleepDuration, StringFormat=\{0\} days}" VerticalAlignment="Center" Margin="15,5,0,5" />
57 
58             <TextBlock FontSize="16pt" Grid.Row="4" Margin="0,2"
59                        Style="{StaticResource Univers55_TextBlock_Style}"
60                        Text="Delay to Unlock"  VerticalAlignment="Center" HorizontalAlignment="Left" />
61             <TextBlock  Grid.Column="1" Grid.Row="4"
62                         HorizontalAlignment="Left"
63                         FontSize="16pt" Foreground="#FF6D6E71"
64                         Style="{StaticResource Univers55_TextBlock_Style}"
65                         Text="{Binding CurrentTool.DelayToUnlock, StringFormat=\{0\} second}" VerticalAlignment="Center" Margin="15,5,0,5" />
66 
67           </Grid>
68           <Grid Margin="0,50,0,0" VerticalAlignment="Center">
69             <Grid.ColumnDefinitions>
70               <ColumnDefinition Width="*" />
71               <ColumnDefinition Width="*" />
72             </Grid.ColumnDefinitions>
73             <Grid.RowDefinitions>
74               <RowDefinition Height="*" />
75             </Grid.RowDefinitions>
76 
77             <Button
78               Grid.Column="1"
79               TabIndex="1"
80               Command="{Binding ShowViewCommand}"
81               CommandParameter="JobSummary"
82               Content="Ok"
83               FontSize="16pt"
84               Style="{StaticResource General_Button_Style}" 
85               BorderBrush="#FF2F2F2F"
86               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
87           </Grid>
88         </StackPanel>
89       </DataTemplate>
View Code
x:Key="ProgramStatus_EqualFalse_View"
 1 <DataTemplate x:Key="ProgramStatus_EqualFalse_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
 2         <Grid x:Name="ProgramFailedPanel">
 3           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,40"><Run Text="Programming failed."/><LineBreak/><Run Text="Please try again"/></TextBlock>
 4           <Grid VerticalAlignment="Bottom" Margin="0,0,0,20">
 5             <Grid.ColumnDefinitions>
 6               <ColumnDefinition Width="*" />
 7               <ColumnDefinition Width="*" />
 8             </Grid.ColumnDefinitions>
 9             <Grid.RowDefinitions>
10               <RowDefinition Height="*" />
11             </Grid.RowDefinitions>
12 
13 
14             <Button
15               Grid.Column="0"
16               TabIndex="1"
17               Command="{Binding ShowViewCommand}"
18               CommandParameter="JobSummary"
19               Content="Cancel"
20               FontSize="16pt"
21               Style="{StaticResource General_Button_Style}" 
22               BorderBrush="#FF2F2F2F" HorizontalAlignment="Left" Width="90" Margin="20,0,0,0"/>
23             <Button
24               Grid.Column="1"
25               TabIndex="1"
26               Command="{Binding ShowViewCommand}"
27               CommandParameter="JobSummary"
28               Content="Retry"
29               FontSize="16pt"
30               Style="{StaticResource General_Button_Style}" 
31               BorderBrush="#FF2F2F2F"
32               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
33           </Grid>
34         </Grid>
35       </DataTemplate>
View Code
x:Key="ProgramStatus_EqualNone_View"
1 <DataTemplate x:Key="ProgramStatus_EqualNone_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
2         <Grid/>
3       </DataTemplate>
View Code

使用的时候这样(要用ContentControl的ContentTemplate来承接上面的DT)

 1 <Grid>
 2         
 3           <ContentControl Content="{Binding}">
 4             <ContentControl.Style>
 5               <Style TargetType="{x:Type ContentControl}">
 6                 <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
 7                 <Style.Triggers>
 8                 <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
 9                     <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualTrue_View}"/>
10                   </DataTrigger>
11                 <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
12                     <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualFalse_View}"/>
13                   </DataTrigger>
14                 <DataTrigger Binding="{Binding ProgramStatus}" Value="{x:Null}">
15                   <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
16                   </DataTrigger>
17                 </Style.Triggers>
18               </Style>
19             </ContentControl.Style>
20           </ContentControl>
21         
22       </Grid>
View Code

这是完整的code

  1 <UserControl x:Class="Halliburton.Wilma.Views.ProgramResultView"
  2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  6              xmlns:vmodel="clr-namespace:Halliburton.Wilma.ViewModels.Main;assembly=Halliburton.Wilma.ViewModels.Main"
  7              d:DesignHeight="620" 
  8              d:DesignWidth="1026"
  9              MinWidth="1026"
 10              MinHeight="620"
 11              mc:Ignorable="d">
 12   <UserControl.Resources>
 13     <ResourceDictionary>
 14       <ResourceDictionary.MergedDictionaries>
 15         <ResourceDictionary Source="/Halliburton.Wilma;component/MainViewResources/IconLibrary.xaml"/>
 16         <!--Add other ResourceDictionaries-->
 17       </ResourceDictionary.MergedDictionaries>
 18       <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
 19 
 20       <!--
 21          <Style x:Key="RetryButton_Style" TargetType="{x:Type ContentControl}">
 22          <Setter Property="Template">
 23          <Setter.Value>
 24          <ControlTemplate TargetType="{x:Type ContentControl}" >
 25          <Button x:Name="RetryButton"
 26          TabIndex="0"
 27          Command="{Binding ShowViewCommand}"
 28          CommandParameter="ConnectTool"
 29          Content="Retry"
 30          FontSize="16pt"
 31          Style="{StaticResource General_Button_Style}" 
 32          BorderBrush="#FF2F2F2F"
 33          />
 34          <ControlTemplate.Triggers>
 35          <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
 36          <Setter TargetName="RetryButton" Property="Visibility" Value="Collapsed" />
 37          </DataTrigger>
 38          <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
 39          <Setter TargetName="RetryButton" Property="Visibility" Value="Visible" />
 40          </DataTrigger>
 41          </ControlTemplate.Triggers>
 42          </ControlTemplate>
 43          </Setter.Value>
 44          </Setter>
 45          </Style>
 46          
 47          <Style x:Key="DisplayProgramStatus_Style" TargetType="{x:Type ContentControl}">
 48          <Setter Property="Template">
 49          <Setter.Value>
 50          <ControlTemplate TargetType="{x:Type ContentControl}" >
 51          <TextBlock  x:Name="DisplayStatus"
 52          HorizontalAlignment="Center"
 53          FontSize="17pt"
 54          FontFamily="/Halliburton.Wilma;component/Fonts/#Univers 65"
 55          Foreground="#FF414142"/>
 56          <ControlTemplate.Triggers>
 57          <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
 58          <Setter TargetName="DisplayStatus" Property="Text" Value="This tool has been successfully programmed." />
 59          </DataTrigger>
 60          <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
 61          <Setter TargetName="DisplayStatus" Property="Text" Value="This tool has not been programmed." />
 62          </DataTrigger>
 63          </ControlTemplate.Triggers>
 64          </ControlTemplate>
 65          </Setter.Value>
 66          </Setter>
 67          </Style>
 68       -->
 69 
 70       <DataTemplate x:Key="ProgramStatus_EqualTrue_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
 71         <StackPanel x:Name="ProgramPassedPanel" Orientation="Vertical">
 72           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" Margin="0,20,0,0"><Run Text="This tool has been"/><LineBreak/><Run Text="successfully programmed."/></TextBlock>
 73 
 74           <Grid KeyboardNavigation.TabNavigation="None" 
 75                 Margin="0,20,0,0" 
 76                 HorizontalAlignment="Center" 
 77                 VerticalAlignment="Center">
 78             <Grid.ColumnDefinitions>
 79               <ColumnDefinition Width="*" />
 80               <ColumnDefinition Width="Auto" />
 81             </Grid.ColumnDefinitions>
 82             <Grid.RowDefinitions>
 83               <RowDefinition Height="*" />
 84               <RowDefinition Height="*" />
 85               <RowDefinition Height="*" />
 86               <RowDefinition Height="*" />
 87               <RowDefinition Height="*" />
 88             </Grid.RowDefinitions>
 89 
 90 
 91             <TextBlock FontSize="16pt" Margin="0,2" Foreground="#FF414142"
 92                        Style="{StaticResource Univers55_TextBlock_Style}" 
 93                        Text="Serial Number"  VerticalAlignment="Center" HorizontalAlignment="Left" />
 94             <TextBlock  Grid.Column="1"
 95                         HorizontalAlignment="Left"
 96                         FontSize="16pt" Foreground="#FF6D6E71"
 97                         Style="{StaticResource Univers55_TextBlock_Style}"
 98                         Text="{Binding CurrentTool.SerialNumber}" VerticalAlignment="Center" Margin="15,5,0,5" />
 99 
100             <TextBlock FontSize="16pt" Grid.Row="1" Margin="0,2"
101                        Style="{StaticResource Univers55_TextBlock_Style}"
102                        Text="Tool Name"  VerticalAlignment="Center" HorizontalAlignment="Left" />
103             <TextBlock  Grid.Column="1" Grid.Row="1"
104                         HorizontalAlignment="Left"
105                         FontSize="16pt" Foreground="#FF6D6E71"
106                         Style="{StaticResource Univers55_TextBlock_Style}"
107                         Text="{Binding CurrentTool.ToolName}" VerticalAlignment="Center" Margin="15,5,0,5" />
108 
109             <TextBlock FontSize="16pt" Grid.Row="2" Margin="0,2"
110                        Style="{StaticResource Univers55_TextBlock_Style}"
111                        Text="Ball Count"  VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Light" />
112             <TextBlock  Grid.Column="1" Grid.Row="2"
113                         HorizontalAlignment="Left"
114                         FontSize="16pt" Foreground="#FF6D6E71"
115                         Style="{StaticResource Univers55_TextBlock_Style}"
116                         Text="{Binding CurrentTool.BallCount}" VerticalAlignment="Center" Margin="15,5,0,5" />
117 
118             <TextBlock FontSize="16pt" Grid.Row="3" Margin="0,2"
119                        Style="{StaticResource Univers55_TextBlock_Style}"
120                        Text="Sleep Duration"  VerticalAlignment="Center" HorizontalAlignment="Left" />
121             <TextBlock  Grid.Column="1" Grid.Row="3"
122                         HorizontalAlignment="Left"
123                         FontSize="16pt" Foreground="#FF6D6E71"
124                         Style="{StaticResource Univers55_TextBlock_Style}"
125                         Text="{Binding CurrentTool.SleepDuration, StringFormat=\{0\} days}" VerticalAlignment="Center" Margin="15,5,0,5" />
126 
127             <TextBlock FontSize="16pt" Grid.Row="4" Margin="0,2"
128                        Style="{StaticResource Univers55_TextBlock_Style}"
129                        Text="Delay to Unlock"  VerticalAlignment="Center" HorizontalAlignment="Left" />
130             <TextBlock  Grid.Column="1" Grid.Row="4"
131                         HorizontalAlignment="Left"
132                         FontSize="16pt" Foreground="#FF6D6E71"
133                         Style="{StaticResource Univers55_TextBlock_Style}"
134                         Text="{Binding CurrentTool.DelayToUnlock, StringFormat=\{0\} second}" VerticalAlignment="Center" Margin="15,5,0,5" />
135 
136           </Grid>
137           <Grid Margin="0,50,0,0" VerticalAlignment="Center">
138             <Grid.ColumnDefinitions>
139               <ColumnDefinition Width="*" />
140               <ColumnDefinition Width="*" />
141             </Grid.ColumnDefinitions>
142             <Grid.RowDefinitions>
143               <RowDefinition Height="*" />
144             </Grid.RowDefinitions>
145 
146             <Button
147               Grid.Column="1"
148               TabIndex="1"
149               Command="{Binding ShowViewCommand}"
150               CommandParameter="JobSummary"
151               Content="Ok"
152               FontSize="16pt"
153               Style="{StaticResource General_Button_Style}" 
154               BorderBrush="#FF2F2F2F"
155               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
156           </Grid>
157         </StackPanel>
158       </DataTemplate>
159 
160       <DataTemplate x:Key="ProgramStatus_EqualFalse_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
161         <Grid x:Name="ProgramFailedPanel">
162           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,40"><Run Text="Programming failed."/><LineBreak/><Run Text="Please try again"/></TextBlock>
163           <Grid VerticalAlignment="Bottom" Margin="0,0,0,20">
164             <Grid.ColumnDefinitions>
165               <ColumnDefinition Width="*" />
166               <ColumnDefinition Width="*" />
167             </Grid.ColumnDefinitions>
168             <Grid.RowDefinitions>
169               <RowDefinition Height="*" />
170             </Grid.RowDefinitions>
171 
172 
173             <Button
174               Grid.Column="0"
175               TabIndex="1"
176               Command="{Binding ShowViewCommand}"
177               CommandParameter="JobSummary"
178               Content="Ok"
179               FontSize="16pt"
180               Style="{StaticResource General_Button_Style}" 
181               BorderBrush="#FF2F2F2F" HorizontalAlignment="Left" Width="90" Margin="20,0,0,0"/>
182             <Button
183               Grid.Column="1"
184               TabIndex="1"
185               Command="{Binding ShowViewCommand}"
186               CommandParameter="ProgramTool"
187               Content="Retry"
188               FontSize="16pt"
189               Style="{StaticResource General_Button_Style}" 
190               BorderBrush="#FF2F2F2F"
191               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
192           </Grid>
193         </Grid>
194       </DataTemplate>
195 
196       <DataTemplate x:Key="ProgramStatus_EqualNone_View" DataType="{x:Type vmodel:ProgramResultViewModel}">
197         <Grid/>
198       </DataTemplate>
199 
200       
201 
202     </ResourceDictionary>
203   </UserControl.Resources>
204 
205   <Grid>
206     <Grid.RowDefinitions>
207       <RowDefinition Height="*"/>
208       <RowDefinition Height="0.09*"/>
209     </Grid.RowDefinitions>
210     <Grid.Background>
211       <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
212         <GradientStop Color="#FFF0EFEF" />
213         <GradientStop Offset="1" Color="#FFD0D0D1" />
214       </LinearGradientBrush>
215     </Grid.Background>
216     <Border HorizontalAlignment="Center" Height="394" Margin="0,50,0,0" VerticalAlignment="Top" Width="585" BorderThickness="4">
217       <Border.BorderBrush>
218         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
219           <GradientStop Color="#FFBBBCBE" Offset="0"/>
220           <GradientStop Color="#FFBBBDBF" Offset="1"/>
221         </LinearGradientBrush>
222       </Border.BorderBrush>
223       <Border.Background>
224         <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
225           <GradientStop Offset="0" Color="#FFD0D2D3" />
226           <GradientStop Offset="1" Color="#FFBBBDBF" />
227         </LinearGradientBrush>
228       </Border.Background>
229 
230       <Grid>
231         
232           <ContentControl Content="{Binding}">
233             <ContentControl.Style>
234               <Style TargetType="{x:Type ContentControl}">
235                 <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
236                 <Style.Triggers>
237                 <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
238                     <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualTrue_View}"/>
239                   </DataTrigger>
240                 <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
241                     <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualFalse_View}"/>
242                   </DataTrigger>
243                 <DataTrigger Binding="{Binding ProgramStatus}" Value="{x:Null}">
244                   <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
245                   </DataTrigger>
246                 </Style.Triggers>
247               </Style>
248             </ContentControl.Style>
249           </ContentControl>
250         
251       </Grid>
252 
253     </Border>
254     <Grid x:Name="BreadCrumb" Grid.Row="1" KeyboardNavigation.TabNavigation="None">
255       <Rectangle x:Name="BreadCurmbBackground" Fill="#FF414041"/>
256       <ContentControl Style="{StaticResource ProgramTool_MainBreadCrumbs_Style}" />
257     </Grid>
258   </Grid>
259 </UserControl>
View Code

 

DT资源里的view tree下会bind到VM里的属性,由于是DP所以会自己向下寻找,但是Button的Command命令最好就bind当前VM object下的command

方法4:ContentControl写在DT里,内部切换ContentTemplate,外部调用用该DT

我之前做的方法是在最上面定义model层而不是vm层简写,而后DT的DataType属性设成了Tool类型,各种Textblockbind到当前vm下面的Tool属性,这样造成button的command binding出现错误,因为在Tool里面没有对应的Command

  1 <UserControl x:Class="Halliburton.Wilma.Views.ProgramResultView"
  2              xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  5              xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  6              xmlns:model="clr-namespace:Halliburton.Wilma.Models.Main;assembly=Halliburton.Wilma.Models.Main"
  7              d:DesignHeight="620" 
  8              d:DesignWidth="1026"
  9              MinWidth="1026"
 10              MinHeight="620"
 11              mc:Ignorable="d">
 12   <UserControl.Resources>
 13     <ResourceDictionary>
 14       <ResourceDictionary.MergedDictionaries>
 15         <ResourceDictionary Source="/Halliburton.Wilma;component/MainViewResources/IconLibrary.xaml"/>
 16         <!--Add other ResourceDictionaries-->
 17       </ResourceDictionary.MergedDictionaries>
 18       <BooleanToVisibilityConverter x:Key="boolToVisibilityConverter" />
 19       
 20       <!--
 21          <Style x:Key="RetryButton_Style" TargetType="{x:Type ContentControl}">
 22          <Setter Property="Template">
 23          <Setter.Value>
 24          <ControlTemplate TargetType="{x:Type ContentControl}" >
 25          <Button x:Name="RetryButton"
 26          TabIndex="0"
 27          Command="{Binding ShowViewCommand}"
 28          CommandParameter="ConnectTool"
 29          Content="Retry"
 30          FontSize="16pt"
 31          Style="{StaticResource General_Button_Style}" 
 32          BorderBrush="#FF2F2F2F"
 33          />
 34          <ControlTemplate.Triggers>
 35          <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
 36          <Setter TargetName="RetryButton" Property="Visibility" Value="Collapsed" />
 37          </DataTrigger>
 38          <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
 39          <Setter TargetName="RetryButton" Property="Visibility" Value="Visible" />
 40          </DataTrigger>
 41          </ControlTemplate.Triggers>
 42          </ControlTemplate>
 43          </Setter.Value>
 44          </Setter>
 45          </Style>
 46          
 47          <Style x:Key="DisplayProgramStatus_Style" TargetType="{x:Type ContentControl}">
 48          <Setter Property="Template">
 49          <Setter.Value>
 50          <ControlTemplate TargetType="{x:Type ContentControl}" >
 51          <TextBlock  x:Name="DisplayStatus"
 52          HorizontalAlignment="Center"
 53          FontSize="17pt"
 54          FontFamily="/Halliburton.Wilma;component/Fonts/#Univers 65"
 55          Foreground="#FF414142"/>
 56          <ControlTemplate.Triggers>
 57          <DataTrigger Binding="{Binding ProgramStatus}" Value="True">
 58          <Setter TargetName="DisplayStatus" Property="Text" Value="This tool has been successfully programmed." />
 59          </DataTrigger>
 60          <DataTrigger Binding="{Binding ProgramStatus}" Value="False">
 61          <Setter TargetName="DisplayStatus" Property="Text" Value="This tool has not been programmed." />
 62          </DataTrigger>
 63          </ControlTemplate.Triggers>
 64          </ControlTemplate>
 65          </Setter.Value>
 66          </Setter>
 67          </Style>
 68       -->
 69 
 70       <DataTemplate x:Key="ProgramStatus_EqualTrue_View" DataType="{x:Type model:Tool}">
 71         <StackPanel x:Name="ProgramPassedPanel" Orientation="Vertical">
 72           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" Margin="0,20,0,0"><Run Text="This tool has been"/><LineBreak/><Run Text="successfully programmed."/></TextBlock>
 73              
 74           <Grid KeyboardNavigation.TabNavigation="None" 
 75                 Margin="0,20,0,0" 
 76                 HorizontalAlignment="Center" 
 77                 VerticalAlignment="Center">
 78             <Grid.ColumnDefinitions>
 79               <ColumnDefinition Width="*" />
 80               <ColumnDefinition Width="Auto" />
 81             </Grid.ColumnDefinitions>
 82             <Grid.RowDefinitions>
 83               <RowDefinition Height="*" />
 84               <RowDefinition Height="*" />
 85               <RowDefinition Height="*" />
 86               <RowDefinition Height="*" />
 87               <RowDefinition Height="*" />
 88             </Grid.RowDefinitions>
 89                         
 90                         
 91             <TextBlock FontSize="16pt" Margin="0,2" Foreground="#FF414142"
 92                        Style="{StaticResource Univers55_TextBlock_Style}" 
 93                        Text="Serial Number"  VerticalAlignment="Center" HorizontalAlignment="Left" />
 94             <TextBlock  Grid.Column="1"
 95                         HorizontalAlignment="Left"
 96                         FontSize="16pt" Foreground="#FF6D6E71"
 97                         Style="{StaticResource Univers55_TextBlock_Style}"
 98                         Text="{Binding SerialNumber}" VerticalAlignment="Center" Margin="15,5,0,5" />
 99                         
100             <TextBlock FontSize="16pt" Grid.Row="1" Margin="0,2"
101                        Style="{StaticResource Univers55_TextBlock_Style}"
102                        Text="Tool Name"  VerticalAlignment="Center" HorizontalAlignment="Left" />
103             <TextBlock  Grid.Column="1" Grid.Row="1"
104                         HorizontalAlignment="Left"
105                         FontSize="16pt" Foreground="#FF6D6E71"
106                         Style="{StaticResource Univers55_TextBlock_Style}"
107                         Text="{Binding ToolName}" VerticalAlignment="Center" Margin="15,5,0,5" />
108                                     
109             <TextBlock FontSize="16pt" Grid.Row="2" Margin="0,2"
110                        Style="{StaticResource Univers55_TextBlock_Style}"
111                        Text="Ball Count"  VerticalAlignment="Center" HorizontalAlignment="Left" FontWeight="Light" />
112             <TextBlock  Grid.Column="1" Grid.Row="2"
113                         HorizontalAlignment="Left"
114                         FontSize="16pt" Foreground="#FF6D6E71"
115                         Style="{StaticResource Univers55_TextBlock_Style}"
116                         Text="{Binding BallCount}" VerticalAlignment="Center" Margin="15,5,0,5" />            
117                                     
118             <TextBlock FontSize="16pt" Grid.Row="3" Margin="0,2"
119                        Style="{StaticResource Univers55_TextBlock_Style}"
120                        Text="Sleep Duration"  VerticalAlignment="Center" HorizontalAlignment="Left" />
121             <TextBlock  Grid.Column="1" Grid.Row="3"
122                         HorizontalAlignment="Left"
123                         FontSize="16pt" Foreground="#FF6D6E71"
124                         Style="{StaticResource Univers55_TextBlock_Style}"
125                         Text="{Binding SleepDuration, StringFormat=\{0\} days}" VerticalAlignment="Center" Margin="15,5,0,5" /> 
126                         
127             <TextBlock FontSize="16pt" Grid.Row="4" Margin="0,2"
128                        Style="{StaticResource Univers55_TextBlock_Style}"
129                        Text="Delay to Unlock"  VerticalAlignment="Center" HorizontalAlignment="Left" />
130             <TextBlock  Grid.Column="1" Grid.Row="4"
131                         HorizontalAlignment="Left"
132                         FontSize="16pt" Foreground="#FF6D6E71"
133                         Style="{StaticResource Univers55_TextBlock_Style}"
134                         Text="{Binding DelayToUnlock, StringFormat=\{0\} second}" VerticalAlignment="Center" Margin="15,5,0,5" />
135                           
136           </Grid>
137           <Grid Margin="0,50,0,0" VerticalAlignment="Center">
138             <Grid.ColumnDefinitions>
139               <ColumnDefinition Width="*" />
140               <ColumnDefinition Width="*" />
141             </Grid.ColumnDefinitions>
142             <Grid.RowDefinitions>
143               <RowDefinition Height="*" />
144             </Grid.RowDefinitions>            
145                     
146             <Button
147               Grid.Column="1"
148               TabIndex="1"
149               Command="{Binding ShowViewCommand}"
150               CommandParameter="AddTool"
151               Content="Ok"
152               FontSize="16pt"
153               Style="{StaticResource General_Button_Style}" 
154               BorderBrush="#FF2F2F2F"
155               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
156           </Grid>
157         </StackPanel>
158       </DataTemplate>
159 
160       <DataTemplate x:Key="ProgramStatus_EqualFalse_View" DataType="{x:Type model:Tool}">
161         <Grid x:Name="ProgramFailedPanel">
162           <TextBlock Style="{DynamicResource Univers65_TextBlock_Style}" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,40"><Run Text="Programming failed."/><LineBreak/><Run Text="Please try again"/></TextBlock>
163           <Grid VerticalAlignment="Bottom" Margin="0,0,0,20">
164             <Grid.ColumnDefinitions>
165               <ColumnDefinition Width="*" />
166               <ColumnDefinition Width="*" />
167             </Grid.ColumnDefinitions>
168             <Grid.RowDefinitions>
169               <RowDefinition Height="*" />
170             </Grid.RowDefinitions>            
171                     
172                             
173             <Button
174               Grid.Column="0"
175               TabIndex="1"
176               Command="{Binding ShowViewCommand}"
177               CommandParameter="JobSummary"
178               Content="Cancel"
179               FontSize="16pt"
180               Style="{StaticResource General_Button_Style}" 
181               BorderBrush="#FF2F2F2F" HorizontalAlignment="Left" Width="90" Margin="20,0,0,0"/>
182             <Button
183               Grid.Column="1"
184               TabIndex="1"
185               Command="{Binding ShowViewCommand}"
186               CommandParameter="JobSummary"
187               Content="Retry"
188               FontSize="16pt"
189               Style="{StaticResource General_Button_Style}" 
190               BorderBrush="#FF2F2F2F"
191               Margin="0,0,20,0" HorizontalAlignment="Right" Width="90"/>
192           </Grid>
193         </Grid>
194       </DataTemplate>
195 
196       <DataTemplate x:Key="ProgramStatus_EqualNone_View" DataType="{x:Type model:Tool}">
197         <Grid/>
198       </DataTemplate>
199 
200       <DataTemplate x:Key="ProgramStatus_CurrentTool_DataTemplate" DataType="{x:Type model:Tool}">
201         <ContentControl Content="{Binding}">
202           <ContentControl.Style>
203             <Style TargetType="{x:Type ContentControl}">
204               <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
205               <Style.Triggers>
206                 <DataTrigger Binding="{Binding IsProgramed}" Value="True">
207                   <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualTrue_View}"/>
208                 </DataTrigger>
209                 <DataTrigger Binding="{Binding IsProgramed}" Value="False">
210                   <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualFalse_View}"/>
211                 </DataTrigger>
212                 <DataTrigger Binding="{Binding IsProgramed}" Value="{x:Null}">
213                   <Setter Property="ContentTemplate" Value="{StaticResource ProgramStatus_EqualNone_View}"/>
214                 </DataTrigger>
215               </Style.Triggers>
216             </Style>
217           </ContentControl.Style>
218         </ContentControl>
219       </DataTemplate>
220 
221     </ResourceDictionary>
222   </UserControl.Resources>
223 
224   <Grid>
225     <Grid.RowDefinitions>
226       <RowDefinition Height="*"/>
227       <RowDefinition Height="0.09*"/>
228     </Grid.RowDefinitions>
229     <Grid.Background>
230       <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
231         <GradientStop Color="#FFF0EFEF" />
232         <GradientStop Offset="1" Color="#FFD0D0D1" />
233       </LinearGradientBrush>
234     </Grid.Background>
235     <Border HorizontalAlignment="Center" Height="394" Margin="0,50,0,0" VerticalAlignment="Top" Width="585" BorderThickness="4">
236       <Border.BorderBrush>
237         <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
238           <GradientStop Color="#FFBBBCBE" Offset="0"/>
239           <GradientStop Color="#FFBBBDBF" Offset="1"/>
240         </LinearGradientBrush>
241       </Border.BorderBrush>
242       <Border.Background>
243         <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,1">
244           <GradientStop Offset="0" Color="#FFD0D2D3" />
245           <GradientStop Offset="1" Color="#FFBBBDBF" />
246         </LinearGradientBrush>
247       </Border.Background>
248             
249       <Grid>
250         <ContentControl Content="{Binding CurrentTool}" 
251                         ContentTemplate="{StaticResource ProgramStatus_CurrentTool_DataTemplate}"/>       
252       </Grid>
253                            
254     </Border>
255     <Grid x:Name="BreadCrumb" Grid.Row="1" KeyboardNavigation.TabNavigation="None">
256       <Rectangle x:Name="BreadCurmbBackground" Fill="#FF414041"/>
257       <ContentControl Style="{StaticResource ProgramTool_MainBreadCrumbs_Style}" />
258     </Grid>
259   </Grid>
260 </UserControl>
View Code

上面虽然不成功,但是提供了另外一种使用ContentControl的实例。

网上其他应用:

http://rachel53461.wordpress.com/2011/05/28/switching-between-viewsusercontrols-using-mvvm/

http://stackoverflow.com/questions/6114479/what-is-the-best-way-to-switch-views-usercontrols-in-mvvm-light-and-wpf

http://stackoverflow.com/questions/6546436/mvvm-how-to-switch-between-views-using-datatemplate-triggers


posted @ 2013-05-06 10:30  若愚Shawn  阅读(690)  评论(0编辑  收藏  举报