2012-10-15 26 views
5

我有以下代碼:我可以綁定反對的WinRT/Windows 8的應用程序商店一DynamicObject

public class MyClass: DynamicObject, INotifyPropertyChanged 
    { 
    Dictionary<string, object> properties = new Dictionary<string, object>(); 

    public override bool TryGetMember(GetMemberBinder binder, out object result) 
    { 
     if (properties.ContainsKey(binder.Name)) 
     { 
      result = properties[binder.Name]; 
      return true; 
     } 
     else 
     { 
      result = "Invalid Property!"; 
      return false; 
     } 
    } 

    public override bool TrySetMember(SetMemberBinder binder, object value) 
    { 
     properties[binder.Name] = value; 
     this.OnPropertyChanged(binder.Name); 
     return true; 
    } 

    public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) 
    { 
     dynamic method = properties[binder.Name]; 
     result = method(args[0].ToString(), args[1].ToString()); 
     return true; 
    } 

    ///.... Rest of the class. 
    } 

當我對着它綁定在XAML中,TryGetMember調試點不triggeret。我想念什麼?

<DataTemplate x:Key="SearchResults"> 
    <Grid Width="294" Margin="6"> 
     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="Auto"/> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 
     <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}" Margin="0,0,0,10" Width="40" Height="40"> 
      <Image Source="{Binding Path=Banner}" Stretch="UniformToFill"/> 
     </Border> 
     <StackPanel Grid.Column="1" Margin="10,-10,0,0"> 
      <TextBlock Text="{Binding SeriesName}" Style="{StaticResource BodyTextStyle}" TextWrapping="NoWrap"/> 
      <TextBlock Text="{Binding Subtitle}" Style="{StaticResource BodyTextStyle}" Foreground="{StaticResource ApplicationSecondaryForegroundThemeBrush}" TextWrapping="NoWrap"/> 
      <TextBlock Text="{Binding Overview}" Style="{StaticResource BodyTextStyle}" Foreground="{StaticResource ApplicationSecondaryForegroundThemeBrush}" TextWrapping="NoWrap"/> 
     </StackPanel> 
    </Grid> 
</DataTemplate> 

的DataContext設置

public ObservableCollection<dynamic> SearchResults {get;set;} 

ICollection col = item.SearchResults;  
this.DefaultViewModel["Results"] = col; //this is the datacontext of the gridview 
+0

我敢打賭,結合是不正確的。顯示XAML綁定以及.... – sll

+0

更新它以包含綁定 –

+0

我包含一個正常的屬性:public string SeriesName {get {return _name; } set {SetProperty(ref _name,value); }}並且綁定正確。 –

回答

0

這裏是一個解決方案,我正在使用一種-的動態綁定。 結合語法,只需包含[]:

public class DynamicLocalSettings : BindableBase 
{ 
    public object this[string name] 
    { 
     get 
     { 
      if (ApplicationData.Current.LocalSettings.Values.ContainsKey(name)) 
       return ApplicationData.Current.LocalSettings.Values[name]; 
      return null; 
     } 
     set 
     { 
      ApplicationData.Current.LocalSettings.Values[name] = value; 
      OnPropertyChanged(name); 
     } 
    } 
} 
+0

你如何引用XAML中的屬性名稱? 'Text =「{Binding Path = [PropertyName]}」'不起作用(掛起設計器)。並且放入引號也不起作用('Text =「{Binding Path = [」PropertyName「]}」')。 – tig

+0

我不在我的電腦,所以我不能嘗試,但你試過在問題 –

+0

的例子後發現設計師的懸掛是另一回事。 'Path = [PropertyName]'工作。 – tig

相關問題