68492

Get one element from an ItemsControl and binding to a Button

Question:

I have an ItemsControl:

<ItemsControl x:Name="myList" ItemTemplateSelector="{DynamicResource mySelectorTemplate}" ItemsPanel="{StaticResource myPanelTemplate}"/>

I want to take one of the items and make a button with his logic:

<Button Style="{StaticResource myButtonStyle}"/>

One of the items of myList has a boolean with true value. This is the item with which the button will be made:

Private myCollection As ObservableCollection(Of Items.Subitems) myList.ItemsSource = myCollection

When myCollection.myBooolean = true, is the field that gives to the item this special treatment.

What would be an efficient way to do this?

Answer1:

There are many possibilitys for this - but this looks like the best way.

You want to use a DataTemplateSelector. You can choose n DataTemplates with this by your own overwritten logic.

<strong><em>C#</em></strong>

Sample Class:

public class MySampleClass { public string Name; public bool MyFlag; }

Sample Template Selector

public class MyTemplateSelector : DataTemplateSelector { public DataTemplate IsTrueTemplate { get; set; } public DataTemplate IsFalseTemplate { get; set; } public override DataTemplate SelectTemplate(object item, DependencyObject container) { MySampleClass temp = item as MySampleClass; if(temp != null) { if (temp.MyFlag) { return IsTrueTemplate; } else { return IsFalseTemplate; } // And so on } else { return base.SelectTemplate(item, container); } } }

Usage in XAML

<ListBox> <ListBox.ItemTemplateSelector> <example:MyTemplateSelector> <example:MyTemplateSelector.IsTrueTemplate> <DataTemplate> <StackPanel> <Button Style="{StaticResource myButtonStyle}" /> <TextBlock Text="{Binding Name}" /> </StackPanel> </DataTemplate> </example:MyTemplateSelector.IsTrueTemplate> <example:MyTemplateSelector.IsFalseTemplate> <DataTemplate> <TextBlock Text="{Binding Name}" /> </DataTemplate> </example:MyTemplateSelector.IsFalseTemplate> </example:MyTemplateSelector> </ListBox.ItemTemplateSelector> </ListBox>

example is your DataTemplateSelector-Namespace. I would recommend you - to store the DataTemplates in the Resources and set it via StaticResource.

Recommend

  • How to Change Panels Position in WPF ItemComtrols?
  • WPF ListBox bind ItemsSource with MVVM-light
  • Why program crashes by adding a lot of images to listview?
  • Need a simple Example of cascading combo boxes using MVVM
  • typeof( ICollection ).GetTypeInfo().IsAssignableFrom( typeof(IList) )
  • How to create a TreeView with child nodes of different UI Controls
  • Shoud view layer know about model classes (entities)?
  • Create Multiselect Listview with Xamarin Forms(Xamarin Cross Platform)
  • Setting a default selected item in ComboBox in WPF MVVM application
  • Change ListView item selection style
  • Using Test Doubles with DbEntityEntry and DbPropertyEntry
  • Access property of DataContext in ItemTemplate
  • Datagrid error - Viemodel property can't be populated
  • WPF Combobox loses Text after Selection
  • Deserialize XML in a WP8 Application
  • Query multiple tables SQLite Windows 10 UWP
  • WPF How to cast ListBox.ItemsSource into ObservableCollection
  • Winforms ListView MouseUp event firing more than once
  • ListView selectedindexchanged
  • Excluding multiple values from the array
  • DataTemplates to display a collection of Points as Ellipses in WPF
  • Return list instead of an item Webservice Phone 8.1 UAP
  • Load Project Settings from .NET Assembly
  • How to use a MultiBinding on DataGridTextColumn?
  • Add items to ComboBox at runtime?
  • Binding ContextMenu Tag to Owner
  • Recursion in ASP.NET Core Razor views
  • Cypher - matching two different possible paths and return both
  • ListItem.Attributes.Add not working
  • Can Jackson SerializationFeature be overridden per field or class?
  • Javascript + PHP Encryption with pidCrypt
  • php design question - will a Helper help here?
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Getting Messege Twice Using IMvxMessenger
  • How can i traverse a binary tree from right to left in java?
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?