How to bind a WPF CustomControl to a ListBox
Posted
by VivyG
on Stack Overflow
See other posts from Stack Overflow
or by VivyG
Published on 2010-05-21T22:30:56Z
Indexed on
2010/05/21
22:50 UTC
Read the original article
Hit count: 444
I've just started to learn WPF this week so please accept my apologies if I am being stupid or missing fundamental points!
Iam trying to build a very simple Contact browser. I have a Collection of Contact objects that displayed in a ListBox control which shows the FullName of the Contact and to the right I have a customControl called BasicContactCard. This is the XAML for the ContacWindow that displays the ListBox:
<DockPanel Width="auto" Height="auto" Margin="8 8 8 8">
<Border Height="56" HorizontalAlignment="Stretch" VerticalAlignment="Top" BorderThickness="1" CornerRadius="8" DockPanel.Dock="Top" Background="Beige">
<TextBox Height="32" Margin="23,5,135,5" Text="Search for contact here" FontStyle="Italic" Foreground="#FFAD9595" FontSize="14" BorderBrush="LightGray"/>
</Border>
<ListBox x:Name="contactList" DockPanel.Dock="Left" Width="192" Height="auto" Margin="5 4 0 8" />
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="0.125*" />
</Grid.RowDefinitions>
<local:BasicContactCard Margin="8 8 8 8" />
<Button Grid.Row="1" x:Name="exit" Content="Exit" HorizontalAlignment="Right" Width="50" Height="25" Click="exit_Click" />
</Grid>
</DockPanel>
and this is the XAML for the CustomControl:
<DockPanel Width="auto " Height="auto" Margin="8,8,8,8">
<Grid Width="auto" Height="auto" DockPanel.Dock="Top">
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<TextBlock x:Name="companyField" Grid.Row="0" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="Company"/>
<TextBlock x:Name="contactField" Grid.Row="1" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="Contact"/>
<TextBlock x:Name="phoneField" Grid.Row="2" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="Phone"/>
<TextBlock x:Name="emailField" Grid.Row="3" Width="auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="8,8,8,8" Text="email"/>
</Grid>
</DockPanel>
The problem I have is how do I bind the individual elements of the CustomControl to the object behind the SelectedItem in the ListBox?
© Stack Overflow or respective owner