wpf - 如何在 itemscontrol 中的数据模板外部添加按钮

标签 wpf xaml datatemplate itemscontrol itemtemplate

我创建了一个 WPF 应用程序。其中我有一个数据模板,如下所示

 <DataTemplate x:Key="ItemTemplate">
    <StackPanel>
        <TextBlock Text="item"/>
        <TextBlock Text="{Binding Number}"/>
    </StackPanel>
 </DataTemplate>

我有一个像这样的 ItemsControl

 <ItemsControl ItemsSource="{Binding Items}"
                      Grid.Column="1"
                      Grid.Row="3"
                      ItemTemplate="{StaticResource ItemTemplateWithButton}" />

我需要这样的项目模板

    <DataTemplate x:Key="ItemTemplateWithButton">
        <StackPanel>
            <StackPanel>
                <TextBlock Text="item"/>
                <TextBlock Text="{Binding Number}"/>
            </StackPanel>
            <StackPanel>
                <Button>
                    <StackPanel>
                        <TextBlock Text="item"/>
                        <TextBlock Text="{Binding Number}"/>
                    </StackPanel>
                </Button>
            </StackPanel>
        </StackPanel>
    </DataTemplate>

是否有可能在新的项目控件中重用数据模板?

最佳答案

您也可以使用 ContentControl

<DataTemplate x:Key="ItemTemplate">
    <StackPanel>
        <TextBlock Text="item"/>
        <TextBlock Text="{Binding Number}"/>
    </StackPanel>
</DataTemplate>
<DataTemplate x:Key="ItemTemplateWithButton">
    <StackPanel>
        <ContentControl ContentTemplate="{StaticResource ItemTemplate}" />
        <Button>
            <ContentControl ContentTemplate="{StaticResource ItemTemplate}" />
        </Button>
    </StackPanel>
</DataTemplate>

关于wpf - 如何在 itemscontrol 中的数据模板外部添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31828449/

相关文章:

c# - x :Name does not exist in the current context,,但其父级确实

xaml - 在带有 MVVM 的 Windows Phone 8 上使用 Pivot 控件进行投标

c# - WinRT Metro 中的 GridView 选择颜色

c# - 如何以编程方式检索用于 UI 层次结构中特定元素的 DataTemplate?

wpf - 慢速组合框性能

wpf - 如何将 Tooltip 的 DataTemplate 与其父级绑定(bind)?

wpf - 如何在 mvvm 中重置 View 模型

wpf - WPF 中的图案画笔

wpf - 如何将 Selenium WebDriver 嵌入为 WPF 控件?

canvas - 如何在长列表选择器中设置数据模板的 z 索引