2

2つのテンプレートがあります。1つはテキストボックス用で、もう1つはリストビュー用です。どちらも、デフォルトの長方形ではなく、角を丸くするために使用されます。私のテキストボックスには、テキストを表示するために「ScrollViewer x:Name = "PART_ContentHost」行が必要でしたが、リストビューでは機能しません。リストビューのテンプレートを取り出すと、サンプルの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>
4

1 に答える 1

1

ControlTemplate にプレゼンターが関連付けられていません。これが、アイテムが表示されない理由です。ListView.ControlTemplate を作成する方法の実例については、このページを参照してください。

MSDN: ListView ControlTemplate の例

コントロール テンプレートの更新された xaml は次のとおりです。

<ControlTemplate x:Key="ListViewTemplate" TargetType="ListView">
    <Border BorderBrush="Black" BorderThickness=".5,1,1,1" CornerRadius="7">
      <ScrollViewer>
        <ItemsPresenter />
      </ScrollViewer>
    </Border>
</ControlTemplate>
于 2012-10-11T19:55:36.200 に答える