简体   繁体   中英

Update databinding for different elements in observable collection

till now I have a observable collection with just one type of objects in it, but now I have second type of objects in it. I have bound some wpf elements to elements of this collection, what I now want is that the input forms change depending on which kind of object it binds to in the collection. What is the best approach to that?

Use a DataTemplate without x:Key , but with DataType={x:Type typename} . Write one data template for each type in your collection. WPF then automatically selects the data template that matches the type of the item in the collection.

Example:

<DataTemplate DataType="{x:Type local:StringType}">
  <TextBox Text="{Binding Text}" />
</DataTemplate>
<DataTemplate DataType="{x:Type local:BooleanType}">
  <CheckBox IsChecked="{Binding Value}" />
</DataTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM