WPF Usercontrol used in ListView

  • Thread starter Thread starter Lloyd Sheen
  • Start date Start date
L

Lloyd Sheen

Hey, I am attempting to move an ItemTemplate to a usercontrol. It is
currently in a listbox and I want to make the content of a listitem a
usercontrol.

I am stuck at how to bind. For the Listbox it is simple , just bind the
ItemsSource. In my mind I should be able to move the template to a
usercontrol and then replace the Listbox.ItemTemplate to use the
usercontrol.

Where I am stuck is with the binding. I currently get the info from Linq to
SQL and then bind it.

The current Listbox XAML code is as follows:

<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="2"
Margin="5">
<StackPanel x:Name="sPanel"
Orientation="Vertical" Width="200" Height="100" >
<Button Click="Button_Click" Tag="{Binding
ElementName=sPanel}" Background="Transparent" >
<Image Source="{Binding
Path=DisplayURI}"
Height="48" Width="48"
/>
</Button>
<TextBlock Text="{Binding Path=DisplayName}"
/>
<Expander x:Name="ShowSongsExpander"
Header="Show Song List" Expanded="ShowSongsExpander_Expanded" Tag="{Binding
ElementName=SongList}">
<ListBox x:Name="SongList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal" >
<TextBlock
Text="{Binding Path=TrackNumber}" Margin="5"></TextBlock>
<TextBlock
Text="{Binding Path=Title}" Margin="5"></TextBlock>
<TextBlock
Text="{Binding Path=Artist}" Margin="5"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>

I have moved it to a usercontrol and now the ItemTemplate looks like:

<ListBox.ItemTemplate>
<DataTemplate>
<local:RFolderCtrl></local:RFolderCtrl>
</DataTemplate>
</ListBox.ItemTemplate>

How do I pass the binding of the object to the usercontrol.

Thanks
Lloyd Sheen
 
Lloyd Sheen said:
Hey, I am attempting to move an ItemTemplate to a usercontrol. It is
currently in a listbox and I want to make the content of a listitem a
usercontrol.

I am stuck at how to bind. For the Listbox it is simple , just bind the
ItemsSource. In my mind I should be able to move the template to a
usercontrol and then replace the Listbox.ItemTemplate to use the
usercontrol.

Where I am stuck is with the binding. I currently get the info from Linq
to SQL and then bind it.

The current Listbox XAML code is as follows:

<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderBrush="Red" BorderThickness="2"
Margin="5">
<StackPanel x:Name="sPanel"
Orientation="Vertical" Width="200" Height="100" >
<Button Click="Button_Click" Tag="{Binding
ElementName=sPanel}" Background="Transparent" >
<Image Source="{Binding
Path=DisplayURI}"
Height="48" Width="48"
/>
</Button>
<TextBlock Text="{Binding
Path=DisplayName}" />
<Expander x:Name="ShowSongsExpander"
Header="Show Song List" Expanded="ShowSongsExpander_Expanded"
Tag="{Binding ElementName=SongList}">
<ListBox x:Name="SongList">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel
Orientation="Horizontal" >
<TextBlock
Text="{Binding Path=TrackNumber}" Margin="5"></TextBlock>
<TextBlock
Text="{Binding Path=Title}" Margin="5"></TextBlock>
<TextBlock
Text="{Binding Path=Artist}" Margin="5"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Expander>
</StackPanel>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>

I have moved it to a usercontrol and now the ItemTemplate looks like:

<ListBox.ItemTemplate>
<DataTemplate>
<local:RFolderCtrl></local:RFolderCtrl>
</DataTemplate>
</ListBox.ItemTemplate>

How do I pass the binding of the object to the usercontrol.

Thanks
Lloyd Sheen

In keeping with almost everything I see about WPF tech as far as VS 2008 is
concerned the compile error wasn't really an error (or so it seems) since
the app runs. It doesn't run well but having a compile error and having the
app run is just plain wrong.

I guess it will be another wait until the next release. I can only think of
the current commercial which promises that the next release won't have the
same problem as the last.

If there is a good book (I haven't found one yet) that explains WPF from the
ground up and deals with NON-TRIVAL examples I would love to hear about it.

Thanks
LS
 
Back
Top