Windows Phone 8 - PriorityBinding

时间:2013-09-20 16:39:53

标签: xaml windows-phone-8

我搜索了文档,但显然Windows Phone 8没有PriorityBinding。在Windows Phone 8中,是否有类似的方法在XAML中实现相同的行为?

我为ListItem创建了一个Style:

<DataTemplate x:Key="ListItem">
    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50" />
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="50" />
        </Grid.ColumnDefinitions>

        <Image 
            Grid.Column="0"
            Source="{Binding Path=ImageSource}" />

        <TextBlock 
            Grid.Column="1" 
            TextWrapping="NoWrap"
            TextTrimming="WordEllipsis"
            Text="{Binding Path=Text}" />

        <Image 
            Grid.Column="2"
            Source="/images/arrow_right.png" />

    </Grid>
</DataTemplate>

现在我想添加一个PriorityBinding,所以如果ImageSource或Text为空,我想添加占位符。

我为WPF找到了这个例子:

<Image.Source>
    <PriorityBinding FallbackValue="/images/default_category.png">
        <Binding Path="ImageSource"/>
    </PriorityBinding>
</Image.Source>

[...]

<TextBlock.Text>
    <PriorityBinding FallbackValue="Placeholder Text">
        <Binding Path="Text"/>
    </PriorityBinding>
</TextBlock.Text>

我尝试在App.xaml中将PriorityBinding添加到我的ImageSource,并发生以下错误:

The type 'PriorityBinding' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

修改

我想将ImageSource绑定到我的模型的ImageSource属性,如果没有数据,那么我想使用占位符作为图像而不是我模型的(空)ImageSource。

对于我的TextBlock也是如此,如果模型中的文本为空,我想显示占位符文本(例如“无数据”)。

1 个答案:

答案 0 :(得分:1)

好吧我认为你需要使用转换器,如果我理解^^

<phone:PhoneApplicationPage.Resources>
    <Converter:TextConverter x:Key="TextConverter"></Converter:TextConverter>
</phone:PhoneApplicationPage.Resources>
 <TextBlock 
            Grid.Column="1" 
            TextWrapping="NoWrap"
            TextTrimming="WordEllipsis"
            Text="{Binding Path=Text,converter{StaticRessource TextConverter}" />

public class TextConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                return "No data";
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }