我可以帮您翻译成中文:在WinRT / Windows 8商店应用程序中,我可以绑定到DynamicObject吗?

5

我有以下代码:

    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中的调试点没有被触发。我有什么遗漏吗?
<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>

数据上下文被设置为
public ObservableCollection<dynamic> SearchResults {get;set;}

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

我敢打赌绑定不正确。同时展示一个XAML绑定... - sll
已更新以包括绑定。 - Poul K. Sørensen
我包含了一个普通属性:public string SeriesName { get { return _name; } set { SetProperty(ref _name, value); } },并且它已经正确地绑定了。 - Poul K. Sørensen
我从未回到这个问题,也记不得我找到了什么解决方案。但下面的答案似乎还可以。 - Poul K. Sørensen
1个回答

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

你如何在XAML中引用属性名称?Text="{Binding Path=[PropertyName]}"无法正常工作(挂起设计师)。而且,放置引号也不起作用(Text="{Binding Path=["PropertyName"]}")。 - tig
我不在电脑旁,所以无法尝试,但是你是否尝试着按照问题中给出的示例进行操作? - Brian Donovan-Smith
结果设计师的悬挂是另一回事。Path=[PropertyName]可行。 - tig

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接