列表框项目将返回字符串时的DataTemplate在Button [英] ListBox items return string, when DataTemplate is Button

查看:97
本文介绍了列表框项目将返回字符串时的DataTemplate在Button的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个WP 8.1 Silverlight应用程序。

I am creating a WP 8.1 Silverlight app.

我有一个的ObservableCollection 字符串名称,即设置为的ItemsSource A 列表框。这是在列表框按钮的名字。那么我想从的ListBox 提取按钮,但是返回值的类型为字符串

I have an ObservableCollection of string names, that is set to the ItemsSource of a ListBox. Which are the names of buttons in the ListBox. I then want to extract the buttons from the ListBox, but the return value is of type string.

在XAML code是:

The xaml code is:

<ListBox x:Name="Game_ScrollViewer_online" Margin="41,104,128,6" SelectedValuePath="Current_game_button">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button x:Name="Current_game_button" Content="{Binding}" 
                    HorizontalAlignment="Center" Height="80" Margin="-14,6,-15,0"
                    VerticalAlignment="Top" Width="210" Template="{StaticResource Button_CurrentLayout1}" 
                    RenderTransformOrigin="0.5,0.5" Foreground="#FFCBECCB" FontFamily="Times New Roman"
                    Click="LoadGame_online" FontSize="16">
            </Button>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

然后我想提取按钮元素:

I then want to extract the button element:

for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++)
{
     var tempType = Game_ScrollViewer_online.Items[i].GetType();
     Button tempBut = (Game_ScrollViewer_online.Items[i] as Button); 
     //Do stuff with button
}

但正如表示,返回类型为字符串。

But as said the return type is string.

为什么不扣?并有以访问按钮的方式?

Why is it not button ? And is there a way to access the button?

推荐答案

您需要<一个href=\"https://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname(v=vs.110).aspx\"相对=nofollow> FrameworkTemplate.FindName方法(String,FrameworkElement的) 为此:

You need FrameworkTemplate.FindName Method (String, FrameworkElement) for this purpose:

private childItem FindVisualChild<childItem>(DependencyObject obj)
where childItem : DependencyObject
{
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
            if (child != null && child is childItem)
                return (childItem)child;
            else
            {
                childItem childOfChild = FindVisualChild<childItem>(child);
                if (childOfChild != null)
                    return childOfChild;
            }
        }
        return null;
}

然后:

for (int i = 0; i < Game_ScrollViewer_online.Items.Count; i++)
{
     ListBoxItem GameListBoxItem = (ListBoxItem)(Game_ScrollViewer_online.ItemContainerGenerator.ContainerFromIndex(i));
     ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(GameListBoxItem);
     DataTemplate myDataTemplate = contentPresenter.ContentTemplate;
     Button tempBut = (Button) myDataTemplate.FindName("Current_game_button", contentPresenter);
     //Do stuff with button
}

要解决缺少 FindName 使用 FindDescendant 是这样的:

To solve missing FindName use FindDescendant like this:

public T FindDescendant<T>(DependencyObject obj) where T : DependencyObject
{
    if (obj is T)
        return obj as T;

    int childrenCount = VisualTreeHelper.GetChildrenCount(obj);
    if (childrenCount < 1)
        return null;

    for (int i = 0; i < childrenCount; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(obj, i);
        if (child is T)
            return child as T;
    }

    for (int i = 0; i < childrenCount; i++)
    {
        DependencyObject child = FindDescendant<T>(VisualTreeHelper.GetChild(obj, i));
        if (child != null && child is T)
            return child as T;
    }

    return null;
}

这篇关于列表框项目将返回字符串时的DataTemplate在Button的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆