Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>To clarify one thing, because Silverlight data-binding isn't strongly typed, the various items in the list don't need to all support a common interface. It's likely a better design if they do, but all they actually need to do is support the properties to which you'll be binding. And if you want to get fancy, you could divide up your datatemplate into different parts, each of which is designed for a different class, and then hide the parts that aren't applicable to the particular item being displayed. So you could have a completely different interface for a drum, guitar, violin, etc. For instance, let's say your instruments were defined like this:</p> <pre><code>public class Drum { public int DrummerCount { get; set; } } public class Violin { public int ViolinistCount { get; set; } } public class Guitar { public int GuitaristCount { get; set; } } </code></pre> <p>You could then create a ListBox that looked like this:</p> <pre><code> &lt;ListBox x:Name="instrumentsListBox"&gt; &lt;ListBox.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;Grid&gt; &lt;TextBlock Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Drum}" Text="{Binding DrummerCount, StringFormat=Drummers:\{0\}, Converter={StaticResource debugConverter}}"/&gt; &lt;TextBlock Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Violin}" Text="{Binding ViolinistCount, StringFormat=Violinists:\{0\}, Converter={StaticResource debugConverter}}"/&gt; &lt;TextBlock Visibility="{Binding Converter={StaticResource instrumentVisibilityConverter}, ConverterParameter=Guitar}" Text="{Binding GuitaristCount, StringFormat=Guitarists:\{0\}, Converter={StaticResource debugConverter}}"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ListBox.ItemTemplate&gt; &lt;/ListBox&gt; </code></pre> <p>Note how each class has a separate TextBlock that's used to display it. The visibility of each TextBlock is controlled via an InstrumentVisibilityConverter that looks like this:</p> <pre><code>public class InstrumentVisibilityConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string expectedType = parameter as string; if (value.GetType().Name == expectedType) { return Visibility.Visible; } else { return Visibility.Collapsed; } } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } </code></pre> <p>And then instantiate it in code like this (or however you want):</p> <pre><code>List&lt;object&gt; instruments = new List&lt;object&gt; { new Guitar(), new Guitar(), new Violin(), new Violin(), new Drum() }; instrumentsListBox.ItemsSource = instruments; </code></pre> <p>Now, I personally think (see <a href="https://stackoverflow.com/questions/1197054/strongly-typed-databinding-in-wpf-silverlight-xaml">here</a>) that Silverlight data-binding <em>should</em> be strongly typed. But I seem to be in the minority there. And as long as it's not, there's no problem with taking advantage of it like this.</p>
 

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