【发布时间】:2018-09-07 17:57:06
【问题描述】:
我正在寻找一种使用 XAML 绑定的方法,它不仅可以绑定到 DependencyProperty,还可以提供设计时视图。
我在后面的代码中有一个 viewmodel 对象的依赖属性:
private static DependencyProperty s_itemProperty
= DependencyProperty.Register("Item", typeof(BadgeItemViewModel), typeof(DetailPage), new PropertyMetadata(null));
public static DependencyProperty ItemProperty
{
get { return s_itemProperty; }
}
在此示例中,绑定没有找到“项目”,但会给我一个后备设计时视图:
<Grid Width="100" Margin="5,20,4,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Ellipse x:Name="elipAvatar" HorizontalAlignment="Center" Height="100" Width="100" VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush ImageSource="{Binding Item.media.badge_image_lg, FallbackValue='https://untappd.akamaized.net/badges/bdg_StPatirkcs2018_lg.jpg'}" Stretch="UniformToFill"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
在此示例中,使用 x:Bind 的运行时绑定效果很好,我可以在我的 XAML 中获得对“Item”对象的 Intellisense 访问权限,但我没有获得回退类型值并且 x:Bind 通常不支持设计时数据:
<Grid Width="100" Margin="5,20,4,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Ellipse x:Name="elipAvatar" HorizontalAlignment="Center" Height="100" Width="100" VerticalAlignment="Center">
<Ellipse.Fill>
<ImageBrush ImageSource="{x:Bind Item.media.badge_image_lg, TargetNullValue='https://untappd.akamaized.net/badges/bdg_StPatirkcs2018_lg.jpg'}" Stretch="UniformToFill"/>
</Ellipse.Fill>
</Ellipse>
</Grid>
是否有可能达成妥协,让我不仅可以访问 XAML 中的 Item 视图模型对象,还可以提供设计时视图或至少为 Image 提供后备值?
谢谢, 瑞克
【问题讨论】: