2015-05-17 26 views
3

我有一個我定義的附加屬性。如何從Windows Store應用程序中的代碼綁定到c#中的自定義附加屬性?

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

現在,我需要建立一個在後面相同的屬性代碼動態綁定,所以我嘗試這樣做:

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

不幸的是,設置綁定的Path屬性會引發一個ArgumentException,聲明:「值不在預期範圍內。」

如果相反,我將「(Grid.Row)」替換爲我的屬性,不會引發異常。

回答

1

在Windows 10進一步的調查表明,這似乎在C#代碼隱藏工作,如果試圖綁定到附加屬性Controls.StateManager.VisualState到同一個名字的在控制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對象作爲唯一的孩子。

0

要調試這個問題,我想,也許我的語法是錯誤的命名空間,所以在我的XAML資源部分,我添加了這個:

<Binding x:Key="yuck" Path="(controls:StateManager.VisualState)" ElementName="pageRoot" /> 

當我檢查的屬性路徑的路徑,這正是正如我指定的那樣。

作爲一種變通方法,我目前得到這樣的結合,從後面的代碼:

BindingOperations.SetBinding(ct, StateManager.VisualStateProperty, 
    Resources["yuck"] as Binding); 

這似乎是工作,但爲什麼我不能從代碼中創建對象的背後?

更新 這個工作在Windows 8的應用程序,但現在給出了UWP錯誤。

爲了讓它在UWP上運行,我不得不將數據綁定到我的ParentGrid的標籤。

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

然後,我可以創建一個綁定的標籤,就像這樣:

var pp3 = new PropertyPath("Tag"); 
barf = new Binding() { Path = pp3, Source = ParentGrid }; 
+0

即使這個黑客不適合我。讓WMC9999擁有一個元素,其中包含一個自定義附加屬性。 –

+0

看起來這裏有一個更確定的「不」: https://stackoverflow.com/questions/39550584/binding-from-code-to-a-custom-attached-property-in-winrt-uwp 但我沒有找到另一個臨時解決方法。 –

相關問題