C# WPF listbox将按钮作为子项时会导致堆栈溢出

C# WPF listbox将按钮作为子项时会导致堆栈溢出,c#,wpf,xaml,listbox,stack-overflow,C#,Wpf,Xaml,Listbox,Stack Overflow,当尝试将按钮作为datatemplate添加到列表框时,我遇到了stackoverflow。当使用文本框时,没有堆栈溢出。这是什么原因造成的?我正在使用Visual Studio 2012更新4 XAML代码: C#代码: 命名空间堆栈溢出测试 { 公共部分类主窗口 { 公共字符串[]CausesStackOverflow{get;set;} 公共主窗口() { CausesStackOverflow=新字符串[]{“Foo”}; 初始化组件(); DataContext=this; } }

当尝试将按钮作为datatemplate添加到列表框时,我遇到了stackoverflow。当使用文本框时,没有堆栈溢出。这是什么原因造成的?我正在使用Visual Studio 2012更新4

XAML代码:


C#代码:

命名空间堆栈溢出测试
{
公共部分类主窗口
{
公共字符串[]CausesStackOverflow{get;set;}
公共主窗口()
{
CausesStackOverflow=新字符串[]{“Foo”};
初始化组件();
DataContext=this;
}
}
}

按钮是一个ContentControl,它的内容也使用DataTemplate。默认的DataTemplate最终以递归方式创建按钮来显示“外部”按钮的内容

您应该明确设置列表框的
ItemTemplate

<ListBox ItemsSource="{Binding CausesStackOverflow}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

按钮是一个ContentControl,它的内容也使用DataTemplate。默认的DataTemplate最终以递归方式创建按钮来显示“外部”按钮的内容

您应该明确设置列表框的
ItemTemplate

<ListBox ItemsSource="{Binding CausesStackOverflow}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Button Content="{Binding}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


我没有“将某些内容绑定到自身”。按钮的内容绑定到数组中的字符串。我没有“将某些内容绑定到自身”。按钮的内容绑定到数组中的字符串。谢谢。这既解决了我的StackOverflow问题,也满足了我的好奇心。谢谢。这既解决了我的StackOverflow问题,也满足了我的好奇心。