Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用DataTemplate以与不使用DataTemplate完全相同的方式显示内容_C#_Wpf_Xaml_Listbox - Fatal编程技术网

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>