c# - 如何为 [] 属性定义 XAML 示例数据

标签 c# xaml windows-phone-8

我正在尝试使用 XAML 示例数据,其中我的“item”类通过为 [] 定义属性访问器来使用动态属性的形式。它(PremiseObject)这样做是这样的:

...
/// <summary>
/// Property accessor. Simulates a dynamic object.
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public object this[string name] {
    get {
        PremiseProperty prop;
        if (_properties.TryGetValue(name, out prop))
            return prop.Value;

        // In XAML <Button Content="Trigger" Command="{Binding [TriggerCommand]}">
        // where 'Trigger' is the name of hte property that is momentary
        if (name.EndsWith("Command")) {
            string cmd = name.Substring(0, name.Length - "Command".Length);
            if (_properties.TryGetValue(cmd, out prop)) {
                return new PremiseCommand(this, cmd);
            }
        }

        return null;
    }
    set {
        SetMember(name, value);
    }
}
... 

别介意中间的粘稠物。关键是在我的 XAML 中我可以做这样很酷的事情:

<ListBox x:Name="DoorsListBox" Margin="0,0,-12,0" 
         ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="0,0,0,35">
                <StackPanel>
                    <!-- We use Description instead of DisplayName because DisplayName changes for GDOs. -->
                    <TextBlock TextAlignment="Left" TextWrapping="NoWrap" Width="320"
                               Text="{Binding [Description]}" Style="{StaticResource PhoneTextTitle2Style}"
                                />
                    <TextBlock TextAlignment="Left" TextWrapping="NoWrap" Width="320"
                               Text="{Binding [GarageDoorStatus], Converter={StaticResource GDOStateFormatConverter}}" 
                               Style="{StaticResource PhoneTextSubtleStyle}"

                               Foreground="{Binding [GarageDoorStatus], Converter={StaticResource GDOStateColorConverter}}"/>
                </StackPanel>
                <Button DataContext="{Binding}" 
                        Content="Trigger" 
                        IsEnabled="True"
                        Style="{StaticResource GDOButtonStyle}"
                        Command="{Binding [TriggerCommand]}">
                </Button>

            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我对这种工作方式非常满意。我已经尝试了几种形式的动态对象,直到我遇到这个问题,它一直工作得非常好。

这个问题是我不知道如何为此指定示例 XAML 数据。

<vm:GarageDoorsViewModel
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:PremiseWP.ViewModels"
    xmlns:prem="clr-namespace:PremiseWebClient"
    IsDataLoaded="False">

    <vm:GarageDoorsViewModel.Items>
        <prem:PremiseObject *** SOMETHING GOES HERE ***/>
    </vm:GarageDoorsViewModel.Items>

</vm:GarageDoorsViewModel>

我需要一些语法来指定示例数据,表示“这是具有此名称和此值的动态属性”。

我该怎么做?这可能吗?

最佳答案

我不确定,但我想你需要为你的类设置 ContentPropertyAttribute 。然后将您的值放入标签内容中。

<vm:GarageDoorsViewModel.Items>
    <prem:PremiseObject>
        <object x:Key="key"/>
    </prem:PremiseObject>
</vm:GarageDoorsViewModel.Items>

关于c# - 如何为 [] 属性定义 XAML 示例数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842310/

相关文章:

javascript - 智能手机屏幕尺寸是多少//智能手机预装了哪些编码语言?

c# - 适用于 Windows 应用商店应用程序的等宽字体

c# - 绑定(bind)运行次数可变的 RichTextBox

windows-phone-7 - Windows Phone 8 SDK Xde 模拟器运行速度非常慢并且崩溃

c# - 有人有在 .net 托管代码中原子地创建临时目录的好方法吗?

c# - 在 WPF 中隐式禁用动画

c# - 当我使用非共享运行时构建 Monodroid 项目时获取编码失败

c# - .designer 文件未与 Visual Studio 中的 .cs 文件关联?

wpf - 控件模板中的模板绑定(bind)

azure - MobileService表存储图片