CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: How to get my custom object in WPF ListBox?

  1. #1
    Join Date
    Feb 2009

    Smile How to get my custom object in WPF ListBox?

    I am trying to get an object from my WPF ListBox. I have a WPF usercontrol(ResultsControl) and it uses a Listbox(lbResults) to Display my custom Objects from a Win form. I am making a new WPF usercontrol(ShowInfoUserControl) to display my custom object's information. The showInfoUserControl should only become visible with the objects's information, when I press on an object in the WPF ListBox. I use Hyperlink to tick the showInfoUserControl. I have tried to lbResults.SelectedItem and lbResults.SelectedValue without luck. Could any one help me please? Every advice will be grateful.

    my method to get my custom objects (code behind for ResultsControl)
    public void GetResults(Result result)
    WPFResult wpfResult = new WPFhResult(result.ObjectId, result.Header, result.Name);


    lbResults.ItemsSource = resultsList;

    private void Windows_Loaded(object sender, RoutedEventArgs e)
    AddHandler(Hyperlink.ClickEvent, (RoutedEventHandler)Infocard_Click);

    method to call the ShowInfoUserControl
    private void Infocard_Click(object sender, RoutedEventArgs e)
    ShowInfoUserControl infocard = new ShowInfoUserControl();

    These are my code behind in ShowInfoUserControl
    public void GetInfocard(Result result)
    Header.Text = result.Header;
    Name.Text = result.Name;

    my Xaml code for ShowInfoUserControl
    <UserControl x:Class="WpfResultsControl.ShowInfoUserControl"
    Height="200" Width="200">
    <TextBlock x:Name="Header" FontFamily="Tahoma" FontSize="8"></TextBlock>
    <TextBlock x:Name="Name" FontFamily="Tahoma" FontSize="4"></TextBlock>

    many thanks in advance

  2. #2
    Join Date
    Jul 2008

    Re: How to get my custom object in WPF ListBox?

    Fireup Blend, create a new WPF Application Project, drop in a ListBox.
    I'm going to grab my ListBoxItems from an external XML file. This could just as easily be a collection in VB.NET/C# (does anybody want/need a post on databinding to CLR Objects?)
    ie: items.xml

    <?xml version="1.0" encoding="utf-8" ?>
    <Items xmlns="">
    <Item>The Fooiest Of Bars</Item>
    <Item>The Bariest of Foos</Item>

    Select the ListBox, Properties, ItemSource, Data Binding, "+ XML", and select items.xml.
    If that works, you should see a bunch of items in the listbox.
    The Animation
    Now we'll have to delve into the XAML code (hit F11), and create a DataTemplate.

    <DataTemplate x:Key="dtListBoxAnimated">
    <TextBlock x:Name="tb1" FontSize="16" Margin="10,10,10,10" Text="{Binding Mode=OneWay, XPath=.}" TextWrapping="Wrap" Foreground="#FFFFFF" />
    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
    <Storyboard Storyboard.TargetName="tb1" Storyboard.TargetProperty="FontSize">
    <DoubleAnimation From="16" To="22" Duration="0:0:0.5"/>
    <Storyboard Storyboard.TargetName="tb1" Storyboard.TargetProperty="FontSize">
    <DoubleAnimation From="22" To="16" Duration="0:0:0.5"/>

    The main thing to focus on is the DataTrigger.EnterActions and ExitActions. This is the actual animation, where EnterActions are when the Trigger is True, and ExitActions are when the Trigger is False.
    In this case, the trigger is the 'IsSelected' property of the "Ancestor" (aka, Parent, which is the ListBoxItem).
    The animation itself will increase the fontsize from 16 to 22 when selected, and then 22 to 16 when deselected.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)