2

在 StandardStyles.xaml 的 DataTemplate 中,我有这个 StackPanel:

<DataTemplate x:Key="Standard160x160ItemTemplate">
    <Grid HorizontalAlignment="Left" Width="160" Height="160">
        ...
        <StackPanel 
            VerticalAlignment="Top" 
            Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
            <TextBlock Text="{Binding UniqueID}" 
                       Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" 
                       Style="{StaticResource TitleTextStyle}" Height="30" Margin="15,0,15,0"/>
        </StackPanel>
        ...
    </Grid>
</DataTemplate>

“uniqueID”是“产品”类的属性:

Public NotInheritable Class Product
    Private Property _sUID As String = String.Empty
    Public Property UniqueID As String
        Get
            Return Me._sUID
        End Get
        Set(value As String)
            Me.SetProperty(Me._sUID, value)
        End Set
    End Property
    ...
End Class

我在这样的网格视图项中使用上面的模板“Standard160x160ItemTemplate”:

                   <GridView Height="210" 
                        x:Name="ItemView"
                        SelectionMode="None"
                        ItemsSource="{Binding Source={StaticResource itemsViewSource}}">
                        <GridViewItem
                            x:Name="GridViewItem"
                            ContentTemplate="{StaticResource Standard160x160ItemTemplate}" 
                            Tapped="GridViewItem_Tapped">
                            <GridViewItem.Style>
                                <Style TargetType="FrameworkElement">
                                    <Setter Property="Margin" Value="0,0,0,0"/>
                                </Style>
                            </GridViewItem.Style>
                        </GridViewItem>
                    </GridView>

这运作良好,并做了它应该做的。

但是,在某些情况下(取决于“产品”对象的其他两个属性,特别是如果其中一个的 UInt 值低于另一个)我想将 StackPanel 的背景更改为纯“红色”而不是“{StaticResource ListViewItemOverlayBackgroundThemeBrush}”。

我不怀疑这是可能的,但我是 XAML 的新手(虽然不是 VB),并且仍然被成千上万的 XAML 标记所淹没,并且真的很难找到解决方案。

所以问题是:如何根据“产品”属性“A”和“B”动态更改模板的背景?

4

1 回答 1

1

最好的方法可能是使用自定义 IValueConverter。这是来自 MSDN 的示例

于 2012-12-09T12:29:13.210 回答