Expression > 論壇首頁 > Expression Blend + SketchFlow > How to obtain the SelectedItem of a ListBox from one screen into another screen?
發問發問
 

提議的解答How to obtain the SelectedItem of a ListBox from one screen into another screen?

  • 2009年11月2日 下午 02:39zarkoh 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Hi folks,

    I found it kinda challenging though the use case is pretty simple.
    Let's say we have a listbox on screen nr. 1 with a binding to some data collection, showing for example one image in each listbox item.
    Now, my intention is when the user selects a listbox item, screen nr. 2 would show up and disply the image of the selected listbox item (from screen nr. 1).
    And yes, I am looking for a solution across screens.

    Any idea how to achieve this?

所有回覆

  • 2009年11月10日 上午 10:09zarkoh 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Ping! Is the above possible at all?
  • 2009年11月13日 下午 08:49D. Matsumoto 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Are you looking for a solution that doesn't require any code-behind?
  • 2009年11月18日 上午 08:20zarkoh 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Any would do good. I couldn't find a way to do it through the Blend UI.
  • 2009年11月18日 下午 05:00D. Matsumoto 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Ok, I'm going to play with this for a few minutes and hopefully I'll get back to you pretty soon.
  • 2009年11月18日 下午 05:41D. Matsumoto 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     提議的解答包含代碼
    Ok, I have a potential solution for you.  If it works, I'd appreciate it if you could give me a vote and mark this as an answer.

    As far as I can tell, with some sort of custom behavior (which I don't know how to write yet), you'll need to use code-behind.  The two key things to do are:

    1. assign your listbox a name, like "mylistbox"
    2. assign a handler for the ListBox's SelectionChanged event
    You can do this with the following XAML:

    <ListBox x:Name="listbox" SelectionChanged="listbox_SelChanged"
     HorizontalAlignment="Left" Margin="8,8,0,8" Width="273" ItemTemplate="{DynamicResource ItemTemplate}" ItemsSource="{Binding Collection}"/>
    The first two attributes are those in #1 and #2 above.  In the code-behind file, you then need to add the handler for SelectionChanged.  The skeleton looks like this:

    public void listbox_SelChanged( object sender, RoutedEventArgs e) {}
    In the code behind, you'll want to figure out which item was selected.  Now, your item might differ from mine because I used a SampleDataCollection from blend to get a listbox with images and text.  Just make sure you know what type it is.  In the handler, you just need to get the ListBox's SelectedItem property, and this will represent whatever is selected.  In my case, it was a SampleDataSource.Item, which has two properties called Property1 and Property2, where Property1 is the image.

    I then created a 2nd window in my app and gave the image control a name, "image".  I then set the image in this window by saying image.Source = (ImageSouce)i.Property1, where i is the SampleDatSource.Item.

    I hope this makes sense.  But if it doesn't, just download my solution and check out the code .  There must be better Blend-ish ways to do this, but I usually fall back to good-ol' code when all else fails.  :)

    I hope this helps!