Image
및 Text
이있는 Button
을 만듭니다. 컨트롤에 두 개의 종속성 속성 인 ImagePath
과 Text
을 추가했으며 컨트롤 템플릿 (Themes \ Generic.xaml)은 이미지와 텍스트를 가로로 정렬하는 간단한 스택 패널입니다.WPF 사용자 지정 컨트롤 : 이미지에 템플릿 바인딩
Text
속성이 정상적으로 작동합니다. 그러나 어떤 이유로 인해 을 ImagePath
종속성 속성에 사용하여 경로를 가져 오면 내 테스트 프로젝트의 샘플 이미지가 나타나지 않습니다. 사용자 지정 컨트롤의 TemplateBinding
을 일시적으로 이미지의 경로로 바꾸어 이미지를 테스트 한 결과이 이미지가 나타납니다.
나는이 분야에서 더 많은 경험을 가진 사람이 왜 컨트롤이 예상대로 작동하지 않는지 살펴보고 왜 좋을까요? 당신의 도움을 주셔서 감사합니다.
내 VS 2008 솔루션에는 CustomControlDemo 프로젝트가 하나 있습니다. 이 프로젝트에는 컨트롤을 테스트하는 데 사용하는 사용자 지정 컨트롤 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>
이미지 경로가 심하지 않은 아이디어 왕? 다시 한번 감사드립니다.