Tôi mới để tạo UserControl và bây giờ tôi đang cố gắng để tùy chỉnh các mẫu UserControl như sau:TemplateBinding hoạt động như thế nào trong Mẫu UserControl?
<UserControl x:Class="WpfApplication1.PieButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" Loaded="OnLoaded">
<UserControl.Template>
<ControlTemplate>
<Path Name="path" Stroke="Aqua" StrokeThickness="3">
<Path.Fill>
<SolidColorBrush Color="{TemplateBinding Fill}" />
</Path.Fill>
<Path.Data>
......
</UserControl>
Đồng thời tôi phải tạo ra các thuộc tính phụ thuộc trong mã back-end:
public partial class PieButton : UserControl
{
public PieButton()
{
InitializeComponent();
}
private void OnLoaded(object sender, RoutedEventArgs e)
{
}
public Color Fill
{
get { return (Color)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Color), typeof(PieButton));
......
Tôi muốn sử dụng TemplateBinding
trong XAML để ràng buộc Fill
thuộc tính của PieButton của tôi để điền vào đối tượng đường dẫn. Visual Studio Designer cảnh báo tôi rằng "thuộc tính Fill không thể truy cập hoặc được nhận diện".
Dựa trên sự hiểu biết của tôi, TemplateBinding
tìm tên thuộc tính từ phần tử áp dụng ControlTemplate này, cần PieControl
tại đây, nhưng tại sao thuộc tính Fill
không thể truy cập tại đây?
BTW,
tôi thử nghiệm các ràng buộc sau đây, và nó có thể làm việc cho tôi
Color="Binding Fill,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type UserControl}}"
Nhưng tôi vẫn nghĩ rằng TemplateBinding nên có thể làm việc theo kịch bản này, vì vậy hãy chỉ ra lỗi của tôi đây. Cảm ơn.
Thank you very much cho việc này. – m4design