2

我创建了一个自定义行为,我可以将属性传递给很好,目前只是字符串,我的行为大致如下所示:

    public class ImageSourceBehavior : Behavior<Image>, INotifyPropertyChanged
{
    public static readonly DependencyProperty ImageDirectoryProperty = DependencyProperty.Register("ImageDirectory", typeof(string), typeof(ImageSourceBehavior), new PropertyMetadata(null));
    public string ImageDirectory
    {
        get
        {
            return (string)GetValue(ImageDirectoryProperty);
        }
        set
        {
            SetValue(ImageDirectoryProperty, value);
        }
    }

    public static readonly DependencyProperty ImageExtensionProperty = DependencyProperty.Register("ImageExtension", typeof(string), typeof(ImageSourceBehavior), new PropertyMetadata(null));
    public string ImageExtension
    {
        get
        {
            return (string)GetValue(ImageExtensionProperty);
        }
        set
        {
            SetValue(ImageExtensionProperty, value);
        }.............
    }

我可以将它与标准字符串一起使用,如下所示:

               <Image x:Name="flag" Stretch="Uniform"  MaxWidth="150" MaxHeight="150">
                                                        <i:Interaction.Behaviors>
                                                            <infrastructure:ImageSourceBehavior ImageDirectory="/Theme_External;component/Media/Images/Flags/" ImageExtension=".png" ImageNameWithoutExtension="some test text" />
                                                        </i:Interaction.Behaviors>
                                                    </Image>

我的问题是,如何传入绑定值而不仅仅是一个字符串,如下所示:

               <Image x:Name="flag" Stretch="Uniform"  MaxWidth="150" MaxHeight="150">
                                                        <i:Interaction.Behaviors>
                                                            <infrastructure:ImageSourceBehavior ImageDirectory="/Theme_External;component/Media/Images/Flags/" ImageExtension=".png" ImageNameWithoutExtension="{Binding Path=.}" />
                                                        </i:Interaction.Behaviors>
                                                    </Image>

当我尝试这个时,我只是得到一个 XAML 解析异常,即使绑定是有效的,有什么想法吗?

谢谢你的时间

4

0 回答 0