How to get my custom object in WPF ListBox?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

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

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    1

    Smile How to get my custom object in WPF ListBox?

    Hey,
    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);

    searchResultsList.Add(wpfSearchResult);

    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();
    infocard.GetInfocard((Result)lbSearchResults.SelectedValues);
    //infocard.GetInfocard((Result)lbSearchResults.SelectedItem);
    }


    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"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:svc="clr-namespace:WpfResultsControl"
    Height="200" Width="200">
    <Grid>
    <StackPanel>
    <TextBlock x:Name="Header" FontFamily="Tahoma" FontSize="8"></TextBlock>
    <TextBlock x:Name="Name" FontFamily="Tahoma" FontSize="4"></TextBlock>
    </StackPanel>
    </Grid>
    </UserControl>

    many thanks in advance

  2. #2
    Join Date
    Jul 2008
    Posts
    24

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

    Method
    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>Foo</Item>
    <Item>Bar</Item>
    <Item>The Fooiest Of Bars</Item>
    <Item>The Bariest of Foos</Item>
    </Items>


    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">
    <WrapPanel>
    <TextBlock x:Name="tb1" FontSize="16" Margin="10,10,10,10" Text="{Binding Mode=OneWay, XPath=.}" TextWrapping="Wrap" Foreground="#FFFFFF" />
    </WrapPanel>
    <DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}, Mode=FindAncestor}}" Value="True">
    <DataTrigger.EnterActions>
    <BeginStoryboard>
    <Storyboard Storyboard.TargetName="tb1" Storyboard.TargetProperty="FontSize">
    <DoubleAnimation From="16" To="22" Duration="0:0:0.5"/>
    </Storyboard>
    </BeginStoryboard>
    </DataTrigger.EnterActions>
    <DataTrigger.ExitActions>
    <BeginStoryboard>
    <Storyboard Storyboard.TargetName="tb1" Storyboard.TargetProperty="FontSize">
    <DoubleAnimation From="22" To="16" Duration="0:0:0.5"/>
    </Storyboard>
    </BeginStoryboard>
    </DataTrigger.ExitActions>
    </DataTrigger>
    </DataTemplate.Triggers>
    </DataTemplate>


    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center