2
votes

I have two templates, one for a textbox and one for a listview, both are just used to give them rounded corners instead of the default rectangle. My textbox needed the "ScrollViewer x:Name="PART_ContentHost" line in order to show text, but that doens't work for the listview. If I take out the Template for the listview the example listviewitem (stuff) will show up. Otherwise it won't and I can't see any other items I add in the code behind. What am I missing in the xaml to get this to work?

Here is my xaml below:

   <!-- 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>
1
You're missing the ContentPresenter in your ListView template. I'm guessing its not needed for your TextBox because it's probably built to place it's content in a component named PART_ContentHostRachel

1 Answers

1
votes

Your ControlTemplate doesn't have a presenter associated with it. This is why you're not seeing any items. See this page for a working example of how to create a ListView.ControlTemplate:

MSDN: ListView ControlTemplate Example

and here's an updated xaml for your control template:

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