Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you have two ways:</p> <p>A. Use a binding converter to convert the image id by looking up your image id from the second list, then bind your ListBox to the list.</p> <p>B. Create a wrapper class that acts as a ViewModel for your data, and bind the ListBox to the ViewModel object.</p> <p><strong>EDIT</strong><br> About using the value converter:<br> If you could make the <code>Users</code> list static or use some Dependency Injection mechanism to obtain the <code>Users</code> list, then your value converter could easily do the conversion.<br> The only other way is somehow pass the Users list from ViewModel (DataContext) to the binding converter.<br> At first I thought that you can set the <code>Users</code> to a property of the converter, like this:</p> <pre><code>&lt;ListBox ItemsSource="{Binding Path=Messages}"&gt; &lt;ListBox.Resources&gt; &lt;c:PosterIDConverter x:Key="pidConverter" Users="..."/&gt; &lt;/ListBox.Resources&gt; ... </code></pre> <p>Or pass it as <code>ConverterParameter</code> to the binding:</p> <pre><code>&lt;TextBlock Text="{Binding Path=Text, Converter={StaticResource pidConverter,ResourceKey}, ConverterParameter=...}"/&gt; </code></pre> <p>But how should I get the <code>Users</code> property from the DataContext? In any of the two above options, you should be able to bind the property to <code>Users</code>, like this (incorrect):</p> <pre><code>&lt;c:PosterIDConverter x:Key="pidConverter" Users="{Binding Path=Users"/&gt; </code></pre> <p>But PosterIDConverter is not DependencyObject and does not support data binding.</p> <h2>Working Solution:</h2> <p>Here is what I finally came to.</p> <p>Your converter class:</p> <pre><code>public class PosterIDConverter : IMultiValueConverter { public object Convert(object[] value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string userId = (string)value[0]; IEnumerable&lt;User&gt; users= (IEnumerable&lt;User&gt;)value[1]; var user = users.FirstOrDefault(u =&gt; u.UserID == userId); if (user != null) return user.ProfilePictureURI; return null; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>Your xaml file (just a sample):</p> <pre><code>&lt;ListBox x:Name="lst" DataContext="{StaticResource vm}" ItemsSource="{Binding Path=Messages}"&gt; &lt;ListBox.Resources&gt; &lt;c:PosterIDConverter x:Key="pidConverter"/&gt; &lt;/ListBox.Resources&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type c:Message}"&gt; &lt;Border BorderBrush="Blue" BorderThickness="1"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="Message: "/&gt; &lt;TextBlock Text="{Binding Path=Text}"/&gt; &lt;/StackPanel&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="URI: "/&gt; &lt;TextBlock&gt; &lt;TextBlock.Text&gt; &lt;MultiBinding Converter="{StaticResource pidConverter}"&gt; &lt;Binding Path="PosterID"/&gt; &lt;Binding ElementName="lst" Path="DataContext.Users"/&gt; &lt;/MultiBinding&gt; &lt;/TextBlock.Text&gt; &lt;/TextBlock&gt; &lt;/StackPanel&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>In this sample I bound the <code>Text</code> property of a <code>TextBlock</code> to the PosterID and <code>Users</code> property at the same time and used the value converter (<a href="http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx" rel="nofollow">IMultiValueConverter</a>) to convert them to image URI.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload