Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从windows应用商店应用程序中的代码隐藏绑定到c#中的自定义附加属性?_C#_Xaml_Windows 8.1 - Fatal编程技术网

如何从windows应用商店应用程序中的代码隐藏绑定到c#中的自定义附加属性?

如何从windows应用商店应用程序中的代码隐藏绑定到c#中的自定义附加属性?,c#,xaml,windows-8.1,C#,Xaml,Windows 8.1,我有一个我定义的附加属性 namespace Controls { public class StateManager : DependencyObject { public static string GetVisualState(DependencyObject obj) { return (string)obj.GetValue(VisualStateProperty); } public static void SetVisualStat

我有一个我定义的附加属性

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中绑定此属性:

<controls:TitleStrip
    controls:StateManager.VisualState=
          "{Binding (controls:StateManager.VisualState), ElementName=pageRoot}" 
    Grid.Column="1"/>
不幸的是,设置绑定的Path属性会引发ArgumentException,声明:“值不在预期范围内。”


相反,如果我用“(Grid.Row)”替换我的属性,则不会引发异常。

为了调试这一点,我认为名称空间的语法可能错误,因此在我的xaml参考资料部分中,我添加了以下内容:

<Binding x:Key="yuck" Path="(controls:StateManager.VisualState)" ElementName="pageRoot" />
这看起来很有效,但是为什么我不能从代码隐藏中创建对象呢

更新 这在Windows8应用程序上有效,但现在在UWP上出现错误

为了让它在UWP上工作,我必须将数据绑定到我的父网格的标记

<Grid x:Name="ParentGrid" Tag="{Binding ElementName=pageRoot, Path=(controls:StateManager.VisualState)}">

对windows 10的进一步调查表明,如果试图将附加属性Controls.StateManager.VisualState绑定到控件ct上同名的附加属性上,这在C#codebehind中似乎有效:

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);
字符串绑定XAML=
@"
";
ResourceDictionary dict=XamlReader.Load(bindingxaml)作为ResourceDictionary;
绑定=dict[“Binding”]作为绑定;
binding.Source=this;
BindingOperations.SetBinding(ct、StateManager.VisualStateProperty、binding);

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

即使这种方法对我也不起作用。在WMC9999中包含一个自定义附加属性的元素。这里似乎有一个更明确的“否”:但我确实找到了另一个临时解决方法。
<Grid x:Name="ParentGrid" Tag="{Binding ElementName=pageRoot, Path=(controls:StateManager.VisualState)}">
var pp3 = new PropertyPath("Tag");
barf = new Binding() { Path = pp3, Source = ParentGrid };
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);