![](/img/trans.png)
[英]TabControl.SelectedItem not a TabItem? RelativeSource binding failing
[英]FindName failing on TabItem
我已經創建了一個自定義控件,其中應用了XAML模板。 在自定義控件中,我需要操作圖像。 為此,我嘗試使用FindName在OnApplyTemplate中查找圖像。 但是,FindName返回null。 編碼:
public class QaTabItem : TabItem
{
public static readonly DependencyProperty HotKeyProperty = DependencyProperty.Register("HotKey", typeof(string), typeof(QaTabItem));
public static readonly DependencyProperty TabImageProperty = DependencyProperty.Register("TabImage", typeof(ImageSource), typeof(QaTabItem));
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(QaTabItem));
public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(QaTabItem));
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register("Orientation", typeof(TabOrientation), typeof(QaTabItem));
public static readonly DependencyProperty IndexProperty = DependencyProperty.Register("Index", typeof(double), typeof(QaTabItem));
private Image arrowImage;
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
arrowImage = base.Template.FindName("ArrowImage", this) as Image;
}
}
模板:
<Style TargetType="{x:Type Image}" x:Key="ImageRotater">
<Setter Property="RenderTransformOrigin" Value="0.5, 0.5" />
<Setter Property="RenderTransform">
<Setter.Value>
<RotateTransform Angle="90" />
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type controls:QaTabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type controls:QaTabItem}">
<Border x:Name="ContentBorder"
Margin="0"
Background="{DynamicResource AiButtonGreyBrush}"
BorderBrush="{DynamicResource AiWhiteBrush}"
BorderThickness="0,.1,0,0">
<Grid HorizontalAlignment="Center" VerticalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="auto" />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
<ColumnDefinition Width="33*" />
</Grid.ColumnDefinitions>
<Label
Grid.Row="0"
Grid.Column="3"
VerticalAlignment="Top"
HorizontalAlignment="Right"
Foreground="{DynamicResource AiGreyBrush}"
FontFamily="Segoe UI"
Content="{TemplateBinding HotKey}" />
<StackPanel
Grid.Row="1"
Grid.Column="0"
Grid.ColumnSpan="2"
Orientation="Horizontal"
HorizontalAlignment="Center">
<Image
Grid.RowSpan="2"
Grid.Column="0"
Stretch="Uniform"
Width="25"
HorizontalAlignment="Left"
Source="{TemplateBinding TabImage}" />
<TextBlock
TextWrapping="Wrap"
Margin="20,0,0,0"
Width="75"
Foreground="{DynamicResource AiWhiteBrush}"
FontSize="14"
Text="{TemplateBinding Title}" />
</StackPanel>
<Label
Grid.Row="2"
Content="{TemplateBinding Caption}"
Grid.Column="0"
Grid.ColumnSpan="2"
VerticalAlignment="Top"
FontSize="10"
FontFamily="Segoe UI"
FontWeight="Bold"
Foreground="{DynamicResource AiGreyBrush}" />
<Image
x:Name="ArrowImage"
Grid.Row="1"
Grid.Column="2"
Source="{DynamicResource TabItemHeaderArrowIcon}"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Width="20"
Height="20" />
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
當我嘗試創建此控件的實例時,為什么OnApplyTemplate中的arrowImage為null?
編輯:我之前沒有提到我將此控件用作其他控件的基類。 當我去使用它時,我將WPF UserControl添加到我的項目中,並將其基類從UserControl更改為QaTabItem。 我實際上沒有嘗試運行代碼,因為設計器中發生了故障。
編輯2:哎呀。 忘了我班上的依賴屬性。
德魯
遺憾的是,如果您使用的setter是密封的,則無法以編程方式在模板或樣式中使用FindName。
來自MSDN https://msdn.microsoft.com/en-uS/office/office365/windows.ui.xaml.setter.property.aspx
如果使用代碼訪問Setter實例,則如果父樣式上的IsSealed
屬性值為true
,則無法更改Setter實例的任何屬性的值。 這也是由單個Setter上的IsSealed
屬性報告的。 當運行時將樣式應用於UI元素並在UI中顯示它們時,系統會將這些屬性設置為true。 嘗試更改密封的Setter會引發運行時錯誤。
但是,一切都不會丟失。 我注意到在你的模板中,控件ArrowImage
有一個DynamicResource
源。 您是否考慮過在代碼中將資源設置為App資源並進行操作的可能性?
要在代碼中設置為App Resource,請將以下行添加到App.xaml.cs
文件構造函數中
App.Current.Resources.Add("TabItemHeaderArrowIcon",null); // <-- Could replace null with a proper image source
然后在您的代碼中,您可以引用App Resource,如下所示,
App.Current.Resources["TabItemHeaderArrowIcon"] = your image source
在應用程序關閉時,請確保使用以下內容清理應用程序資源(因為它在內存中分配)
App.Current.Resources.Remove("TabItemHeaderArrowIcon")
這是一種過去對我有用的方法。 希望它也會對你有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.