c# - 如何在 XAML 中访问我的 ListBox 的 DataTemplate(但不是绑定(bind))中的 TextBlock?

标签 c# xaml windows-phone-8 datatemplate visualtreehelper

XAML

<ListBox x:Name="lsbQueue" Margin="0,0,0,10" Grid.RowSpan="2" Loaded="lsbQueue_Loaded" SelectionChanged="lsbQueue_SelectionChanged" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" ItemsSource="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel x:Name="stk" Orientation="Vertical">
                <!-- This is the bugger which I need to access behind the scenes-->
                <TextBlock x:Name="tbActive" FontSize="35" FontFamily="Segoe UI Symbol" Text="" Height="115" Margin="0,0,0,-110" Tag="Active"/>
                <!-- -->
                <TextBlock Text="{Binding Path=SongName}" FontSize="35" Width="388" FontWeight="Normal" Margin="60,0,0,0"/>
                <TextBlock Width="390" FontWeight="Thin" Margin="60,-5,0,10" Opacity="0.55">
                            <Run Text="{Binding Artist}" />
                            <Run Text=", " /> <!-- space -->
                            <Run Text="{Binding Album}" />
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

上面是我的列表框,它是借助以下代码从后面的代码填充的:

C#

void GetQueue()
{
    var songs = new List<song>();

    for (int i = 0; i < MediaPlayer.Queue.Count; i++)
    {
        songs.Add(new song {
            SongName = MediaPlayer.Queue[i].Name.ToString(),
            Album = MediaPlayer.Queue[i].Album.Name.ToString(),
            Artist = MediaPlayer.Queue[i].Artist.ToString()
        });

    }
    lsbQueue.ItemsSource = songs.ToList();
    //lsbQueue.SelectedValue.ToString();
    GlobalVars._song = MediaPlayer.Queue.ActiveSongIndex;
    lsbQueue.SelectedIndex = GlobalVars._song;
    // .......
}

public class song
{
    public string SongName { get; set; }
    public string Album { get; set; }
    public string Artist { get; set; }
}

public class Song : List<song>
{
    public Song()
    {
        Add(new song { 
            SongName = "", 
            Album = "",
            Artist = ""
        });
    }
}

我试过使用 VisualTreeHelper 和其他扩展方法,可以在这里找到:

GeekChamp

Falafel Blog

但是我没有成功。我几乎已经放弃了。有没有人知道可以做什么。谢谢。

enter image description here

如您所见 - 我可以成功获取媒体队列,但我想在“SelectedItem”的左侧显示视觉提示,就像 TextBlock 中的播放角色 - tbActive。希望这对您有所帮助!

最佳答案

<TextBlock>是您尝试使用 GeekChamp 教程中提供的函数访问的 DataTemplate 中的第一个条目。

<ListBox x:Name="lb" SelectionChanged="lb_SelectionChanged"/>

// namespaces
using System.Windows.Media;

private T FindFirstElementInVisualTree<T>(DependencyObject parentElement) where T : DependencyObject
{
    var count = VisualTreeHelper.GetChildrenCount(parentElement);
    if (count == 0)
        return null;

    for (int i = 0; i < count; i++)
    {
        var child = VisualTreeHelper.GetChild(parentElement, i);

        if (child != null && child is T)
        {
            return (T)child;
        }
        else
        {
            var result = FindFirstElementInVisualTree<T>(child);
            if (result != null)
                return result;
        }
    }
    return null;
}

private void lb_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    // get the ListBoxItem by SelectedIndex OR index number
    //ListBoxItem lbi = (ListBoxItem) this.lb.ItemContainerGenerator.ContainerFromIndex(lb.SelectedIndex);

    // get the ListBoxItem by SelectedItem or object in your ViewModel
    ListBoxItem lbi = (ListBoxItem)this.lb.ItemContainerGenerator.ContainerFromItem(lb.SelectedItem);

    // get your textbox that you want
    TextBlock tbActive= FindFirstElementInVisualTree<TextBlock>(lbi);
}

关于c# - 如何在 XAML 中访问我的 ListBox 的 DataTemplate(但不是绑定(bind))中的 TextBlock?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25974753/

相关文章:

c# - 模型中的 ASP.NET CORE DI

c# - .NET Native 比使用 ReadAsync 调用的调试构建慢得多

wpf - 需要WPF工具包控件的主题(尤其是DataGrid)

image-processing - 为 Windows Phone 开发 Polyframe 图像编辑应用程序

c# - 如何在启动时更改起始页?

c# - C#中构造函数的具体形式

c# - 我的 Azure DocumentDB 文档类是否应该继承自 Microsoft.Azure.Documents.Document?

c# - global::在 C# 中代表什么

c# - WPF - 根据附加属性的值重用具有不同设置的 UserControl

windows-phone-8 - 无法打开此消息