我正在使用ContentControl更改顯示在我的UI中的控件,具體取決於用戶通過DataTemplateSelector選擇的對象類型。卸載控件時無法獲取附加行爲屬性值
當控件被卸載時,基於替換下面的ContentControl中的內容,我無法再訪問 附加行爲的附加行爲的Unloaded事件處理程序的屬性。
例如,如果用戶點擊鑽取單位然後點擊用戶界面中的燃料卡車單位,那麼我無法訪問控件中的附加屬性。它就像控制器卸載時一樣,在DateTemplateSelector激發時讀取附加屬性 已經太晚了。
如果控件因爲關閉父窗口或其他原因(如阻止父文檔選項卡)而被卸載,那麼我讀取附加的行爲屬性值時沒有問題。
這是錯誤的代碼片段:
的代碼試圖找到附加的行爲屬性:「行爲:PersistUiBehavior.PersistanceChildCode」從下面 和我期待的「結果」等於17或18取決於控件處於活動狀態時。
public static void GetPersistenceRequiredControls(DependencyProperty property, DependencyObject root, int code, List<object> sources)
{
if ((property != null) && (root != null))
{
// property = behaviours:PersistUiBehavior.PersistanceChildCode does not exist here when unloaded
var result = (int) root.GetValue(property);
if (result == code)
{
sources.Add(root);
}
}
}
這裏是一個XAML片段顯示的各個部分:
<UserControl x:Class="FleetControl.Editors.Views.PropertyEditor">
<UserControl.Resources>
<ResourceDictionary>
<templates:PropertyEditorTemplateSelector x:Key="PropertyEditorTemplateSelector">
<templates:PropertyEditorTemplateSelector.DrillUnitEditorTemplate>
<DataTemplate>
<telerik:RadPropertyGrid
x:Name="DrillPropertyGrid"
behaviours:PersistUiBehavior.FileName="Test1.txt"
behaviours:PersistUiBehavior.PersistanceParentCode="5"
behaviours:PersistUiBehavior.PersistanceChildCode="17"
behaviours:PersistUiBehavior.Name="DrillPropertyGrid">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition DisplayName="Equipment Ident" Binding="{Binding DisplayName, Mode=OneWay}" GroupName="Description" IsReadOnly="True" />
<telerik:PropertyDefinition DisplayName="Equipment Description" Binding="{Binding Description, Mode=OneWay}" GroupName="Description" IsReadOnly="True"/>
<telerik:PropertyDefinition DisplayName="Equipment Type Ident" Binding="{Binding EquipmentType.Ident, Mode=OneWay}" GroupName="Description" IsReadOnly="True"/>
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
</DataTemplate>
</templates:PropertyEditorTemplateSelector.DrillUnitEditorTemplate>
<templates:PropertyEditorTemplateSelector.FuelTruckUnitEditorTemplate>
<DataTemplate>
<telerik:RadPropertyGrid
x:Name="FuelTruckUnitPropertyGrid"
behaviours:PersistUiBehavior.FileName="Test2.txt"
behaviours:PersistUiBehavior.PersistanceParentCode="6"
behaviours:PersistUiBehavior.PersistanceChildCode="18"
behaviours:PersistUiBehavior.Name="FuelTruckUnitPropertyGrid">
<telerik:RadPropertyGrid.PropertyDefinitions>
<telerik:PropertyDefinition DisplayName="Location" Binding="{Binding Location.LocationName, Mode=OneWay}" GroupName="Realtime" IsReadOnly="True"/>
<telerik:PropertyDefinition DisplayName="Location Code" Binding="{Binding Location.Code, Mode=OneWay}" GroupName="Realtime" IsReadOnly="True"/>
<telerik:PropertyDefinition DisplayName="Location Description" Binding="{Binding Location.Description, Mode=OneWay}" GroupName="Realtime" IsReadOnly="True"/>
</telerik:RadPropertyGrid.PropertyDefinitions>
</telerik:RadPropertyGrid>
</DataTemplate>
</templates:PropertyEditorTemplateSelector.FuelTruckUnitEditorTemplate>
</templates:PropertyEditorTemplateSelector>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ContentControl
ContentTemplateSelector="{StaticResource PropertyEditorTemplateSelector}"
Content="{Binding Path=SelectedPropertyEditor, Mode=OneWay}" />
</Grid>
</UserControl>