【问题标题】:Dynamically change a styled background color; XAML/VB in Visual Studio Express 2012 for Windows 8动态更改样式背景颜色;适用于 Windows 8 的 Visual Studio Express 2012 中的 XAML/VB
【发布时间】:2012-12-09 08:51:25
【问题描述】:

在 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”动态更改模板的背景?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2014-04-05
      • 2013-09-10
      • 1970-01-01
      相关资源
      最近更新 更多