2
我有如下因素代碼,我能夠重構:WPF崩VS重新綁定
<HierarchicalDataTemplate DataType = "{x:Type data:FieldMapping}"
ItemsSource = "{Binding Path=FieldMappingItems}">
<Grid Name="MainGrid" >
<Grid.ToolTip >
<TextBlock Name="Tt" Text="{Binding Path=ToolTip}"/>
</Grid.ToolTip>
<Grid.ColumnDefinitions >
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<TextBlock Name="Error" Margin="0,0,5,0" TextElement.FontWeight="Bold" TextElement.Foreground="Red" Visibility="Collapsed" Text="!"/>
<StackPanel Grid.Column="1" >
<Image Source="../../icons\column.png" Name="Image"/>
<Image Source="../../icons\Connect.png" Name="SourceImage" Visibility="Collapsed"/>
<Image Source="../../icons\Service.png" Name="AttritubeImage" Visibility="Collapsed"/>
<Image Source="../../icons\table.png" Name="SearchImage" Visibility="Collapsed"/>
<Image Source="../../icons\surrogateKey.png" Name="TargetImage" Visibility="Collapsed"/>
<Image Source="../../icons\Key16.png" Name="PrimayKeyImage" Visibility="Collapsed"/>
<Image Source="../../icons\tempTable.png" Name="TempTableImage" Visibility="Collapsed"/>
<Image Source="../../icons\Component16.png" Name="SPImage" Visibility="Collapsed"/>
<Image Source="../../icons\DatabaseSelectCellSecure16.png" Name="RootImage" Visibility="Collapsed"/>
<Image Source="../../icons\Console16.png" Name="ScriptImage" Visibility="Collapsed"/>
<Image Source="../../icons\CodeWindowSelect16.png" Name="ProgramImage" Visibility="Collapsed"/>
</StackPanel>
<TextBlock Name="tb" Margin="2,0,0,0" Grid.Column="2" Text="{Binding Path=FieldName}"/>
<TextBlock Name="tbSource" Margin="2,0,0,0" Visibility="Collapsed" Grid.Column="2" Text="{Binding Path=FullNodePath}"/>
</Grid>
<DataTemplate.Triggers>
<MultiDataTrigger >
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Published}" Value="False"/>
<Condition Binding="{Binding Path=FieldMappingType}" Value="TempTable"/>
</MultiDataTrigger.Conditions>
<Setter TargetName="tb" Property="FontStyle" Value="Italic"/>
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter TargetName="MainGrid" Property="Opacity" Value="0.5"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsPrimaryKey}" Value="True">
<Setter TargetName="tb" Property="Margin" Value="4,0,0,0"/>
<Setter TargetName="Image" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="PrimayKeyImage" Property="Visibility" Value="Visible"/>
<Setter TargetName="SourceImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="TargetImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="RootImage" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=FieldMappingType}" Value="Table">
<Setter TargetName="tb" Property="Margin" Value="4,0,0,0"/>
<Setter TargetName="Image" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="SourceImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="PrimayKeyImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="TargetImage" Property="Visibility" Value="Visible"/>
<Setter TargetName="RootImage" Property="Visibility" Value="Collapsed"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=FieldMappingType}" Value="TempTable">
<Setter TargetName="tb" Property="Margin" Value="4,0,0,0"/>
<Setter TargetName="Image" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="SourceImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="PrimayKeyImage" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="TempTableImage" Property="Visibility" Value="Visible"/>
<Setter TargetName="RootImage" Property="Visibility" Value="Collapsed"/>
<!--<Setter TargetName="tb" Property="Margin" Value="15,0,0,0"/>-->
</DataTrigger>
</HierarchicalDataTemplate>
正如你可以看到有大量的圖片被倒塌後,根據不同的類型的對象,顯示期望的圖像。
我明白,這是一個爛攤子,我在下面的方式重構它:
<Grid Name="MainGrid">
<Grid.ToolTip>
<TextBlock Name="Tt" Text="{Binding Path=ToolTip}" />
</Grid.ToolTip>
<StackPanel Orientation="Horizontal">
<TextBlock x:Name="Error" Margin="0,0,5,0" TextElement.FontWeight="Bold" TextElement.Foreground="Red" Visibility="{Binding Path=Warning, Converter={StaticResource BooleanToVisibility}}" Text="!"/>
<Image x:Name="Icon" Source="../../icons\column.png" />
<TextBlock x:Name="tb" Margin="2,0,0,0" Text="{Binding Path=FieldName}" />
</StackPanel>
</Grid>
<DataTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=Published}" Value="False" />
<Condition Binding="{Binding Path=FieldMappingType}" Value="TempTable" />
</MultiDataTrigger.Conditions>
<Setter TargetName="tb" Property="FontStyle" Value="Italic" />
</MultiDataTrigger>
<DataTrigger Binding="{Binding Path=Active}" Value="False">
<Setter TargetName="MainGrid" Property="Opacity" Value="0.5" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=IsPrimaryKey}" Value="True">
<Setter TargetName="tb" Property="Margin" Value="4,0,0,0" />
<Setter TargetName="Icon" Property="Source" Value="../../Icons/Key16.png" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=FieldMappingType}" Value="TempTable">
<Setter TargetName="tb" Property="Margin" Value="4,0,0,0" />
<Setter TargetName="Icon" Property="Source" Value="../../Icons/tempTable.png" />
</DataTrigger>
雖然我的重構是更容易消化,更succint,我在想,如果我通過改變獲得更好的性能綁定單個圖像而不是切換多個圖像的可視性。
你有任何文件來支持這個,因爲這看起來非常直觀。 – d347hm4n
沒有文檔,這是根據經驗。每次我們切換標籤頁時,應用程序初稿都凍結了3秒,這幾乎不可用。第二稿使用隱藏/顯示而不是交換ViewModels,它是瞬時的。 – Contango
在你描述的場景中,這似乎是一個合理的預期。我想知道對象是否會被存儲在內存中呢?如果您在切換開關上看到即時響應,我會期待他們。因爲這些都是樹狀視圖中的所有節點,並且我可以有很多節點。我不想分配太多的記憶,因爲他們的狀態不會經常變化。 – d347hm4n