简体   繁体   English

如何访问列表中的文本块

[英]how to acess the textblock in list

  1. I have a TextBlock in the list which我在列表中有一个 TextBlock
  2. I need to change the style of text我需要更改文本样式
  3. I need to change the background colour of the TextBlock我需要更改 TextBlock 的背景颜色

My problem is I can't change both text and image if anyone knows about this, please help me to solve this problem.我的问题是我不能同时更改文本和图像,如果有人知道这一点,请帮助我解决这个问题。

<ListView.ItemTemplate>
    <DataTemplate>
        <Grid>
            <TextBlock  x:Name="student" text="default"
                        FontSize="14"
                        FontWeight="SemiBold"
                        Foreground="blue"
            </TextBlock>
            
        </Grid>
    </DataTemplate>
</ListView.ItemTemplate> 

You could use VisualTreeHelper to find the child.您可以使用VisualTreeHelper来找到孩子。

Xaml: Xaml:

 <ListView  x:Name="MyListView" SelectionChanged="MyListView_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <TextBlock 
                        x:Name="student"      
                        Text="default"
                        FontSize="14"
                        FontWeight="SemiBold"
                        Foreground="blue"></TextBlock>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

Code-behind:代码隐藏:

  private void MyListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // get selected listviewitem
        ListViewItem item = MyListView.ContainerFromItem(MyListView.SelectedItem) as ListViewItem;
        // the DependencyObject is the TextBlock that you want to get
        DependencyObject targetelement = FindChild(item, "student");

        TextBlock selectedText = targetelement as TextBlock;
        selectedText.Foreground = new SolidColorBrush(Colors.Red);

        if (e.RemovedItems.Count>0) 
        {
            // change unselected item color 
            ListViewItem removedItem = MyListView.ContainerFromItem(e.RemovedItems.FirstOrDefault()) as ListViewItem;
            DependencyObject removedelement = FindChild(removedItem, "student");

            TextBlock removedText = removedelement as TextBlock;
            removedText.Foreground = new SolidColorBrush(Colors.Blue);
        }
    }

    public DependencyObject FindChild(DependencyObject parant,string name)
    {
        int count = VisualTreeHelper.GetChildrenCount(parant);

        for (int i = 0; i < count; i++)
        {
            var MyChild = VisualTreeHelper.GetChild(parant, i);
            if (MyChild is FrameworkElement && ((FrameworkElement)MyChild).Name == name)
                return MyChild;

            var FindResult = FindChild(MyChild, name);
            if (FindResult != null)
                return FindResult;
        }
        return null;
    }

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

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