如何从 Windows 商店应用程序中的代码后面绑定到 c# 中的自定义附加属性? [英] How can I bind to a custom attached property in c# from code behind in a windows store app?

查看:22
本文介绍了如何从 Windows 商店应用程序中的代码后面绑定到 c# 中的自定义附加属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个附加属性.

I have an attached property I defined.

namespace Controls
{
public class StateManager : DependencyObject
{
    public static string GetVisualState(DependencyObject obj)
    {
        return (string)obj.GetValue(VisualStateProperty);
    }

    public static void SetVisualState(DependencyObject obj, string value)
    {
        obj.SetValue(VisualStateProperty, value);
    }

    // Using a DependencyProperty as the backing store for VisualStateProperty.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty VisualStateProperty =
        DependencyProperty.RegisterAttached("VisualState", typeof(string), typeof(StateManager),
        new PropertyMetadata(null,
            (s, e) => {
                var stateName = (string)e.NewValue;
                var ctrl = s as Control;
                if (ctrl == null) throw new InvalidCastException("You can only attach VisualState properties to Controls");

                if (!string.IsNullOrEmpty(stateName))
                    VisualStateManager.GoToState(ctrl, stateName, true);
            }));
}
}

我可以像这样在 XAML 中绑定到这个属性:

I can bind to this property in XAML Like this:

<controls:TitleStrip
    controls:StateManager.VisualState=
          "{Binding (controls:StateManager.VisualState), ElementName=pageRoot}" 
    Grid.Column="1"/>

现在,我需要在后面的代码中动态地创建一个绑定到相同的属性,所以我尝试了这个:

Now, I need to create a binding dynamically in code behind to the same property, so I tried this:

var pp = new PropertyPath("(controls:StateManager.VisualState)");
var binding = new Binding() { Path= pp, Source=this };
BindingOperations.SetBinding(ct, StateManager.VisualStateProperty, binding);

不幸的是,设置绑定的 Path 属性会引发 ArgumentException 声明:值不在预期范围内."

Unfortunately, setting the Path property of the binding, throws an ArgumentException stating: "Value does not fall within the expected range."

如果相反,我将(Grid.Row)"替换为我的属性,则不会引发异常.

If instead, I substitute "(Grid.Row)" for my property, no exception is thrown.

推荐答案

对 Windows 10 的进一步调查表明,如果尝试将附加属性 Controls.StateManager.VisualState 绑定到控制ct上的同名:

Further investigation on windows 10 shows that this appears to work in C# codebehind, if trying to bind to the attached property Controls.StateManager.VisualState onto the Attached Property of the same name on the control ct:

string bindingxaml =
@"<ResourceDictionary
xmlns:controls=""using:Controls""
xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
>
<Binding x:Key=""binding"" Path=""(controls:StateManager.VisualState)"" />
</ResourceDictionary>";

ResourceDictionary dict = XamlReader.Load(bindingxaml) as ResourceDictionary;
Binding binding = dict["binding"] as Binding;
binding.Source = this;
BindingOperations.SetBinding(ct, StateManager.VisualStateProperty, binding);

奇怪的是,如果您不将其包含在 ResourceDictionary 中,并尝试将 Binding 对象创建为唯一子对象,则会引发异常.

Strangely, this throws exceptions if you don't enclose it in a ResourceDictionary, and try to create the Binding object as the only child.

这篇关于如何从 Windows 商店应用程序中的代码后面绑定到 c# 中的自定义附加属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆