我嘗試製作圖像按鈕用戶控件。但是,當我在窗口初始化設置圖像,然後拋出此異常。這裏是我的代碼未將對象引用設置爲對象的實例
考試。
<UserControl.Resources>
<Style x:Key="ImageButton" TargetType="{x:Type Button}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<StackPanel>
<Image x:Name="ButtonImage" Stretch="None"/>
<ContentPresenter Margin="0,8,0,0" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<ControlTemplate.Triggers>
<Trigger Property="IsFocused" Value="True"/>
<Trigger Property="IsDefaulted" Value="True"/>
<Trigger Property="IsMouseOver" Value="True"/>
<Trigger Property="IsPressed" Value="True"/>
<Trigger Property="IsEnabled" Value="False"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Button x:Name="ProcestaImageButton" Content="TextBlock" Style="{DynamicResource ImageButton}"/>
C#代碼。
public ImageSource ButtonImage
{
get { return ((this.ProcestaImageButton.Template).FindName("ButtonImage", this.ProcestaImageButton) as Image).Source; }
set { ((this.ProcestaImageButton.Template).FindName("ButtonImage", this.ProcestaImageButton) as Image).Source = value; }
}
請告訴我我錯在哪裏。請爲我提供一個示例。 感謝您的幫助。
使用您的調試器...設置斷點並查看哪個對象爲空 –