模板WPF XAML中的附加属性

本文关键字:属性 WPF XAML 模板 | 更新日期: 2023-09-27 18:15:26

我有附加属性和ControlTemplate使用附加属性。附加属性:

using System.Windows;
namespace NoteProjectV2.classes.frmtopicclasses
{
    class togglebuttonimage : DependencyObject
    {
        public static readonly DependencyProperty togglebuttonimagesource = DependencyProperty.RegisterAttached("ImageSource", typeof(string), typeof(togglebuttonimage), new PropertyMetadata(default(string)));

        public static void Settogglebuttonimagesource(UIElement element, string value)
        {
            element.SetValue(togglebuttonimagesource, value);
        }
        public static string Gettogglebuttonimagesource(UIElement element)
        {
            return (string)element.GetValue(togglebuttonimagesource);
        }

    }
}

这是我的控件模板(我在togglebutton中使用了它)

<Application x:Class="NoteProjectV2.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:NoteProjectV2"
             xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses"
             StartupUri="frmTopic.xaml">
    <Application.Resources>
        <Style x:Key="togglebutton_topic_menu_normal" TargetType="ToggleButton">
            <Setter Property="Width" Value="40" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Border Name="border">
                            <Border.Style>
                                <Style>
                                    <Setter Property="Border.Background" Value="Black"/>
                                </Style>
                            </Border.Style>
                            <Image Width="22" Height="22" Name="image" >
                                <Image.Style>
                                    <Style>
   Only This is not working===============>  <Setter Property="Image.Source" Value="{Binding Path=(m:togglebuttonimage.togglebuttonimagesource),RelativeSource={RelativeSource TemplatedParent}}" />
                                    </Style>
                                </Image.Style>
                            </Image>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Application.Resources>
</Application>

我在代码中使用附加属性:我还在

上面添加了名称空间
xmlns:m="clr-namespace:NoteProjectV2.classes.frmtopicclasses"
<ToggleButton Style="{StaticResource togglebutton_topic_menu_normal}" m:togglebuttonimage.togglebuttonimagesource="accept.png" />

但这不是工作我的问题在哪里?

模板WPF XAML中的附加属性

DependencyProperty类的RegisterRegisterAttached方法的第一个参数是该属性的名称。

当您使用名称"ImageSource"时,它实际上应该是"ToggleButtonImageSource"(已经使用了适当的大小写)。还请注意,只要您只声明附加的属性,就不需要从DependencyObject派生出所属类。

public class ToggleButtonImage
{
    public static readonly DependencyProperty ToggleButtonImageSourceProperty =
        DependencyProperty.RegisterAttached(
            "ToggleButtonImageSource", typeof(string), typeof(ToggleButtonImage));
    public static void SetToggleButtonImageSource(UIElement element, string value)
    {
        element.SetValue(ToggleButtonImageSourceProperty, value);
    }
    public static string GetToggleButtonImageSource(UIElement element)
    {
        return (string)element.GetValue(ToggleButtonImageSourceProperty);
    }
}

除此之外,您最好使用ImageSource而不是string作为属性的类型。