簡體   English   中英

選擇ListViewItem時如何更改文本塊的顏色?

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

在Windows 8.1應用商店中選擇ListViewItem時,如何更改文本塊的顏色?

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

設置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>

您可以在SelctionChanged事件中更改顏色:

請參閱下面的鏈接:

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>

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