动态更改样式背景颜色; Visual Studio Express 2012 for Windows 8中的XAML / VB

时间:2012-12-09 08:51:26

标签: vb.net xaml visual-studio-2012

在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>

这很有效并且应该做到。

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

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

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

1 个答案:

答案 0 :(得分:1)

最好的方法可能是使用自定义IValueConverter。 Here's the example from MSDN