【发布时间】:2014-02-18 00:56:31
【问题描述】:
我正在尝试使用 XAML 示例数据,其中我的“项目”类通过为 [] 定义属性访问器来使用一种动态属性形式。它 (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>
我需要一些语法来指定“这是具有此名称和此值的动态属性”的示例数据。
我该怎么做?有没有可能?
【问题讨论】:
标签: c# xaml windows-phone-8