とを使用して、WPFカスタムコントロールを作成してButton
いImage
ますText
。ImagePath
コントロールに2つの依存関係プロパティを追加しましText
た。コントロールテンプレート(Themes \ Generic.xaml内)は、画像とテキストを水平方向に配置する単純なスタックパネルです。
Text
プロパティは正常に動作します。しかし、何らかの理由で、依存関係プロパティを使用TemplateBinding
してパスを取得すると、テストプロジェクトのサンプル画像が表示されません。カスタムコントロールのを一時的に画像へのパスにImagePath
置き換えて画像をテストしました。その場合、画像が表示されます。TemplateBinding
この分野の経験が豊富な人が、コントロールが期待どおりに機能しない理由を調べて教えてくれることを願っています。ご協力いただきありがとうございます。
私のVS2008ソリューションには、CustomControlDemoという1つのプロジェクトが含まれています。プロジェクトには、カスタムコントロールTaskButton.csと、コントロールのテストに使用するメインウィンドウWindow1.xamlが含まれています。私のテスト画像calendar.pngは、プロジェクトのルートレベルのResourcesフォルダーにあり、Generic.xamlは、同じくプロジェクトのルートレベルのThemesフォルダーにあります。
これが私のカスタムコントロールのコードです(TaskButton.csから):
using System.Windows;
using System.Windows.Controls;
namespace CustomControlDemo
{
public class TaskButton : RadioButton
{
#region Fields
// Dependency property backing variables
public static readonly DependencyProperty ImagePathProperty;
public static readonly DependencyProperty TextProperty;
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
static TaskButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TaskButton), new FrameworkPropertyMetadata(typeof(TaskButton)));
// Initialize ImagePath dependency properties
ImagePathProperty = DependencyProperty.Register("ImagePath", typeof(string), typeof(TaskButton), new UIPropertyMetadata(null));
TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(TaskButton), new UIPropertyMetadata(null));
}
#endregion
#region Dependency Property Wrappers
/// <summary>
/// The ImagePath dependency property.
/// </summary>
public string ImagePath
{
get { return (string)GetValue(ImagePathProperty); }
set { SetValue(ImagePathProperty, value); }
}
/// <summary>
/// The Text dependency property.
/// </summary>
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
#endregion
}
}
そして、これが(Generic.xamlからの)コントロールテンプレートです:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomControlDemo">
<Style TargetType="{x:Type local:TaskButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:TaskButton}">
<StackPanel Height="Auto" Orientation="Horizontal">
<Image Source="{TemplateBinding ImagePath}" Width="24" Height="24" Stretch="Fill"/>
<TextBlock Text="{TemplateBinding Text}" HorizontalAlignment="Left" Foreground="{DynamicResource TaskButtonTextBrush}" FontWeight="Bold" Margin="5,0,0,0" VerticalAlignment="Center" FontSize="12" />
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
最後に、コントロールのテストに使用しているWindow1マークアップを次に示します。
<Window x:Class="CustomControlDemo.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customControl="clr-namespace:CustomControlDemo"
Title="Window1" Height="300" Width="300">
<Grid>
<customControl:TaskButton ImagePath="Resources\calendar.png" Text="Calendar" />
</Grid>
</Window>
画像パスが機能しない理由はありますか?再度、感謝します。