How to access to a textblock of datatemplate of a listview

asked 2020-04-23T20:59:15.607+00:00
Shay Wilner 1,406 Reputation points

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

Universal Windows Platform (UWP)
No comments
{count} votes

Accepted answer
  1. answered 2020-04-24T01:50:46.897+00:00
    Fay Wang - MSFT 5,121 Reputation points

    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);
    }
    
    No comments

0 additional answers

Sort by: Most helpful