XAML 当DataTemplate为Button时,ListBox项返回字符串

xxb16uws  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(82)

我正在创建WP 8.1 Silverlight应用程序。
我有一个字符串名称ObservableCollection,它被设置为ListBoxItemsSource。这是ListBox中按钮的名称。然后我想从ListBox中提取按钮,但返回值的类型是string
xaml代码是:

<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>

然后我想提取button元素:

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
}

但正如前面所说,返回类型是string。
为什么不是纽扣?有办法打开按钮吗?

djmepvbi

djmepvbi1#

编辑:Game_ScrollViewer_online.ItemContainerGenerator.ContainerFromIndex(i)已删除,读取here.
使用Game_ScrollViewer_online.ContainerFromIndex(i)代替,请阅读此处
为此,您需要FrameworkTemplate.FindName Method (String, FrameworkElement)

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,如下所示:

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;
}

相关问题