如何在选择ListViewItem时更改Textblock颜色?

时间:2016-09-30 16:33:06

标签: c# windows-store-apps windows-8.1-universal

如何在Windows 8.1商店应用中选择ListViewItem时更改文本块的颜色?

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding text}" Name="Mytxt" Foreground="Black"></TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

3 个答案:

答案 0 :(得分:1)

设置ListViewItem样式并在选择时更改颜色:

  <Style x:Key="{x:Type ListViewItem}" TargetType="ListViewItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Grid SnapsToDevicePixels="true" Background="Transparent">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <DoubleAnimation 
                                                Storyboard.TargetName="buttonBackgroundShape" 
                                                Storyboard.TargetProperty="Opacity" To="1" Duration="0"/>
                                </Storyboard>
                            </VisualState>

                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>

                    <Rectangle Name="buttonBackgroundShape" Stretch="Fill" Opacity="0" Fill="Red" Height="50" SnapsToDevicePixels="True" />
                    <ContentPresenter x:Name="buttonText" Margin="30,0,30,0" TextBlock.FontSize="12pt" Content="{Binding Path=Name}" VerticalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

答案 1 :(得分:0)

您可以更改SelctionChanged事件中的颜色:

请参阅下面的链接:

https://msdn.microsoft.com/fr-fr/library/windows/apps/windows.ui.xaml.controls.primitives.selector.selectionchanged.aspx

答案 2 :(得分:0)

<ListView>
    <ListViewItem Name="listViewItem1" Selected="listViewItem1_Selected">
        <TextBlock Text="{Binding text}" Name="Mytxt"/>
    </ListViewItem>
</ListView>

private void listViewItem1_Selected(object sender, RoutedEventArgs e)
{
    Mytxt.Foreground = Brushes.Red;
    Mytxt.Background = Brushes.Green;
}