gpt4 book ai didi

c# - 为其设置模板时,我的 ListView 项目未显示

转载 作者:太空宇宙 更新时间:2023-11-03 13:54:10 24 4
gpt4 key购买 nike

我有两个模板,一个用于文本框,一个用于 ListView ,两者都只是用来给它们圆角而不是默认的矩形。我的文本框需要“ScrollViewer x:Name="PART_ContentHost"行才能显示文本,但这对 ListView 不起作用。如果我取出 ListView 的模板,示例 listviewitem(东西)将显示出来。否则它不会,而且我看不到我在后面的代码中添加的任何其他项目。我在 xaml 中缺少什么才能让它工作?

下面是我的 xaml:

   <!-- Design Templates to set the borders of the controls-->
<UserControl.Resources>
<ControlTemplate x:Key="TextBoxTemplate" TargetType="TextBox">
<Border BorderBrush="Black" BorderThickness="1,1,1,.5" CornerRadius="7">
<ScrollViewer x:Name="PART_ContentHost" ></ScrollViewer>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="ListViewTemplate" TargetType="ListView">
<Border BorderBrush="Black" BorderThickness=".5,1,1,1" CornerRadius="7">
</Border>
</ControlTemplate>
</UserControl.Resources>

<!-- Controls -->
<Grid Height="270" Width="400">
<StackPanel Width="390">
<TextBox Height="35" Name="InputTextbox" Template="{StaticResource TextBoxTemplate}" VerticalContentAlignment="Center" TextChanged="InputTextbox_TextChanged"></TextBox>
<ListView Height="235" Name="ResultsListView" Template="{StaticResource ListViewTemplate}" SelectionChanged="ResultsListView_SelectionChanged">
<ListViewItem Content="stuff"></ListViewItem>
</ListView>
</StackPanel>
</Grid>

最佳答案

您的 ControlTemplate 没有与之关联的演示者。这就是您没有看到任何项目的原因。有关如何创建 ListView.ControlTemplate 的工作示例,请参阅此页面:

MSDN: ListView ControlTemplate Example

这是您的控件模板的更新 xaml:

<ControlTemplate x:Key="ListViewTemplate" TargetType="ListView">
<Border BorderBrush="Black" BorderThickness=".5,1,1,1" CornerRadius="7">
<ScrollViewer>
<ItemsPresenter />
</ScrollViewer>
</Border>
</ControlTemplate>

关于c# - 为其设置模板时,我的 ListView 项目未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12846804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com