Silverlight 3 - 通过 XAML 将绑定值传递给行为

发布于 2024-08-20 03:15:07 字数 2332 浏览 3 评论 0原文

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

    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 解析异常,有什么想法吗?

感谢您抽出时间

I have created a custom behavior that I can pass properties to fine, currently just strings, my behavior looks roughly like this:

    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);
        }.............
    }

I can use it fine with standard strings like so:

               <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>

My question is, how do I pass in a binding value instead of just a string, like this:

               <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>

I just get a XAML parse exception when I try this, even though the binding is valid, any ideas?

Thanks for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文