question

65010773 avatar image
0 Votes"
65010773 asked ·

How to access to a textblock of datatemplate of a listview

Hi
I have a listview


  <ListView Style="{StaticResource ListViewStyle1}" x:Name="Dgvresult"    Height="600" Width="580" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,16,0,0" Visibility="Visible">
 <ListView.ItemTemplate>
       <DataTemplate x:DataType = "local:Classdatah1" x:Name="dtp">
          <Grid Width="540" BorderThickness="2,2,2,2" >
             <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                 </Grid.ColumnDefinitions>
        
       <TextBlock x:Name="cvd" Grid.Column="0" Text="{x:Bind Path=civildate}" Margin="12,6,0,0" 
       Style="{ThemeResource BaseTextBlockStyle}"   Foreground="White"/>
   <TextBlock x:Name="hwd"  Grid.Column="1" Grid.Row="0" Text="{x:Bind Path=hwholedate}" Margin="50,6,0,0" 
 Style="{ThemeResource BodyTextBlockStyle}" Foreground="White" />
  <TextBlock x:Name="np"  Grid.Column="2" Grid.Row="0" Text="{x:Bind Path=namep}" Margin="290,6,0,0" 
 Style="{ThemeResource BodyTextBlockStyle}"  Foreground="White"  />
  </Grid>
   </DataTemplate>
   </ListView.ItemTemplate>
   </ListView>

by code i fill the listview by biding to a ObservableCollection

i would like to access by code to the item(6) to modify the foreground of its each textblock

Thanks

windows-uwp
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

FayWang-MSFT avatar image
1 Vote"
FayWang-MSFT answered ·

Hello,

Welcome to Microsoft Q&A!

If you want to modify the foreground of each textblock, it's better to use Binding. You can declare a SolidColorBrush property in your Classdatah1 model which you will use to bind to the foreground of the textblock. If the three textblocks use the same foreground, you just need to declare a property, but if the three textblocks uses three different foregrounds, you need to declare three properties to bind with them. I take the first case as an example, suppose they always have the same foreground color.


.cs:

 public class Classdatah1 : INotifyPropertyChanged
 {
     public event PropertyChangedEventHandler PropertyChanged = delegate { };
     ......
     private SolidColorBrush foregroundColor;
     public SolidColorBrush ForegroundColor {
         get {
             return foregroundColor;
         }
         set {
             foregroundColor = value;
             OnPropertyChanged();
         }
     }

     public void OnPropertyChanged([CallerMemberName] string propertyName = null)
     {
         this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
     }
 }

.xaml:



 <TextBlock x:Name="cvd" Grid.Column="0" Text="{x:Bind Path=civildate}" Margin="12,6,0,0" Foreground="{x:Bind ForegroundColor,Mode=OneWay}"/>
 <TextBlock x:Name="hwd" Grid.Column="1" Grid.Row="0" Text="{x:Bind Path=hwholedate}" Margin="50,6,0,0" Foreground="{x:Bind ForegroundColor,Mode=OneWay}" />
 <TextBlock x:Name="np"  Grid.Column="2" Grid.Row="0" Text="{x:Bind Path=namep}" Margin="290,6,0,0" Foreground="{x:Bind ForegroundColor,Mode=OneWay}"  /> 

Suppose you want to change the foreground color when you click an item,


 private void Dgvresult_ItemClick(object sender, ItemClickEventArgs e)
 {
     Classdatah1 model = e.ClickedItem as Classdatah1;
     model.ForegroundColor = new SolidColorBrush(Colors.Red);
 }

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.