2016-09-30 7 views
0

Как изменить цвет текстового блока при выборе ListViewItem в приложении для магазина Windows 8.1?Как изменить цвет Textblock при выборе ListViewItem?

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

я нашел подобный вопрос: http://stackoverflow.com/questions/20379234/change-the-foreground-color-of-a-textblock-inside-a-listviews-datatemplate -when – Mikewhat

+0

Но это не работает – Mikewhat

ответ

1

Set 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> 
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; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^