简体   繁体   English

选择ListViewItem时如何更改文本块的颜色?

[英]How to change Textblock color when the ListViewItem is selected?

How to change the textblock's color when when the ListViewItem is selected in the Windows 8.1 store app? 在Windows 8.1应用商店中选择ListViewItem时,如何更改文本块的颜色?

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

Set ListViewItem Style and change the color when selected: 设置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>

You can change the Color in the SelctionChanged Event : 您可以在SelctionChanged事件中更改颜色:

Refer to the Link Below : 请参阅下面的链接:

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

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

and

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM