C# 使用DataTemplate以与不使用DataTemplate完全相同的方式显示内容
我的列表框当前按预期使用绑定C# 使用DataTemplate以与不使用DataTemplate完全相同的方式显示内容,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,我的列表框当前按预期使用绑定 <ListBox ItemsSource="{Binding ChildDuplicate, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding}" Height="Auto" > 其中ChildDuplicate是一个可观察的集合 我面临的问题是我需要为列表框中的每个项目添加一个按钮(因此它同时显示我的MyUserControl和button控件) 由于多种原因,此按钮无法在M
<ListBox ItemsSource="{Binding ChildDuplicate, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding}" Height="Auto" >
其中ChildDuplicate是一个可观察的集合
我面临的问题是我需要为列表框中的每个项目添加一个按钮(因此它同时显示我的MyUserControl和button控件)
由于多种原因,此按钮无法在MyUserControl中使用
我希望使用DataTemplate,但这似乎不起作用(抛出XmlParser错误),我已经尝试过了
<ListBox ItemsSource="{Binding ChildDuplicate, UpdateSourceTrigger=PropertyChanged}" DataContext="{Binding}" Height="Auto" >
<ListBoxItem>
<DataTemplate>
<ContentControl Content="{Binding}"></ContentControl>
</DataTemplate>
</ListBoxItem>
</ListBox>
我还尝试以同样的方式使用ItemsControl控件,同样的问题
知道我如何使用DataTemplate以与不使用DataTemplate完全相同的方式显示内容吗?试试以下方法:
<ListBoxItem>
<ListBoxItem.Template>
<ControlTemplate>
<ContentControl Content="{Binding}"></ContentControl>
</ControlTemplate>
</ListBoxItem.Template>
</ListBoxItem>