Tôi đang tạo điều khiển tùy chỉnh WPF, Button
với Image
và Text
. Tôi đã thêm hai thuộc tính phụ thuộc vào điều khiển, ImagePath
và Text
và mẫu điều khiển (trong Chủ đề \ Generic.xaml) là một bảng ngăn xếp đơn giản sắp xếp hình ảnh và văn bản theo chiều ngang.Điều khiển tùy chỉnh WPF: TemplateBinding to Image
Tài sản Text
hoạt động tốt. Nhưng vì lý do nào đó, hình ảnh mẫu trong dự án thử nghiệm của tôi không xuất hiện khi tôi sử dụng TemplateBinding
đến thuộc tính phụ thuộc ImagePath
để có được đường dẫn của nó. Tôi đã thử nghiệm hình ảnh bằng cách tạm thời thay thế TemplateBinding
trong điều khiển tùy chỉnh bằng đường dẫn đến hình ảnh, trong trường hợp này nó xuất hiện.
Tôi hy vọng rằng ai đó có nhiều kinh nghiệm trong lĩnh vực này có thể xem và cho tôi biết lý do kiểm soát không hoạt động như mong đợi. Cảm ơn bạn đã giúp đỡ.
Giải pháp VS 2008 của tôi chứa một dự án, CustomControlDemo. Dự án có một điều khiển tùy chỉnh, TaskButton.cs và một cửa sổ chính, Window1.xaml, mà tôi sử dụng để kiểm tra điều khiển. Hình ảnh thử nghiệm của tôi, calendar.png, nằm trong thư mục Tài nguyên ở cấp cơ sở của dự án và Generic.xaml nằm trong thư mục Chủ đề, cũng ở cấp cơ sở của dự án.
Đây là đoạn mã để điều khiển tùy chỉnh của tôi (từ 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
}
}
Và đây là sự kiểm soát mẫu (từ 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>
Và cuối cùng, đây là đánh dấu Window1 mà tôi đang sử dụng để kiểm tra điều khiển:
<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>
Mọi ý tưởng tại sao đường dẫn hình ảnh không phải là wor nhà vua? Cảm ơn một lần nữa.