title: How do I get values from list of object based on selected value in Wpf
body: <p><strong>Updated with a simple example in the end</strong></p>
  
  <p>I have a list of objects that are rendered into a canvas. This objects holds another object that are an base class </p>
  
  <pre><code>public class FooBase()
  {
  }
  
  public class Foo1 : FooBase()
  {
  }
  
  public class Foo2 : FooBase()
  {
     public ICollection&lt;Bar&gt; BarList() { get;set; }
  }
  
  public class Bar
  {
     public BarIdentifier BarIdentifier {get; set;}
     public int Value1 {get; set;}
     public string Value2 {get; set; }
  }
  
  public class HolderClass()
  {
     public FooBase FooClass() { get; set; }
  
     public int PosX {get;set;}
     public int PosY {get;set;}
  }
  </code></pre>
  
  <p>In my Model I also have an selection of an active Bar class. This is set by using a dropdown. I also have a List of HolderClasses</p>
  
  <pre><code>public BarIdentifier SelectedBar {get;set;}
  
  public List&lt;ValveSystemOverviewGridContent&gt; HolderList {get;set;}
  </code></pre>
  
  <p>In my markup/xaml I have this</p>
  
  <pre><code>&lt;ItemsControl ItemsSource="{Binding HolderList}"&gt;
      &lt;ItemsControl.ItemsPanel&gt;
          &lt;ItemsPanelTemplate&gt;
              &lt;Canvas /&gt;
          &lt;/ItemsPanelTemplate&gt;
      &lt;/ItemsControl.ItemsPanel&gt;
      &lt;ItemsControl.ItemContainerStyle&gt;
          &lt;Style TargetType="ContentPresenter"&gt;
              &lt;Setter Property="Canvas.Left" Value="{Binding Path=XPos, Mode=OneWay}" /&gt;
              &lt;Setter Property="Canvas.Top" Value="{Binding Path=YPos, Mode=OneWay}" /&gt;
          &lt;/Style&gt;
      &lt;/ItemsControl.ItemContainerStyle&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>and the datatemplates as this</p>
  
  <pre><code>&lt;UserControl.Resources&gt;
      &lt;DataTemplate DataType="{x:Type entities:Foo1 }" &gt;
          &lt;Border BorderBrush="Black" BorderThickness="1"&gt;
              &lt;StackPanel Orientation="Horizontal" &gt;
                  &lt;Label Content="FooClass 1" /&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
      &lt;DataTemplate DataType="{x:Type entities:Foo2 }"&gt;
          &lt;Border CornerRadius="4" Background="White" BorderThickness="2" BorderBrush="{DynamicResource NormalBorderBrush}" &gt;
              &lt;StackPanel Orientation="Vertical"&gt;
                  &lt;Label Content="Foo Class 2" /&gt;
  
                  &lt;Label Content="{Binding ????" /&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/DataTemplate&gt;
  
      &lt;DataTemplate DataType="{x:Type entities:HolderClass }"&gt;
          &lt;StackPanel Orientation="Horizontal"  &gt;
              &lt;Label Content="This is Holder Class" FontWeight="Bold" /&gt;
              &lt;ContentControl Content="{Binding Path=FooClass}" /&gt;
          &lt;/StackPanel&gt;
      &lt;/DataTemplate&gt;
  
  &lt;/UserControl.Resources&gt;
  </code></pre>
  
  <p>What I would like to do in the FooBar2 datatemplate is to get one of the items in BarList that matches to the SelectedBar. This would I do normally by using</p>
  
  <pre><code>BarList.First(b =&gt; b.BarIdentifier.Id == modelView.SelectedBar.Id);
  </code></pre>
  
  <p>Is there any way to accomplish this filter/selection in my data template and display the Value1 and Value2 of Bar in effective way?</p>
  
  <p>If I change the dropdown in main view I would like to display the Bar that matches that BarIdentifier</p>
  
  <p><strong>Update with example</strong></p>
  
  <p>As requested I tried to do a short example. Included a function GetCalcDataForCondition how to filter out the CalcData based on selected Condition.</p>
  
  <p>MainWindow.xaml.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace FilterSample
  {
      public class CalcData
      {
          public Condition Condition { get; set; }
          public int Diameter { get; set; }
          public string Name { get; set; }
      }
      public class Condition
      {
          public int Id { get; set; }
          public string Name { get; set; }
      }
      public class BaseClass
      {
          public string Name { get; set; }
      }
  
      class TagClass : BaseClass
      {
      }
  
      class ConnectionClass : BaseClass
      {
          public List&lt;CalcData&gt; CalcData { get; set; }
      }
  
      public class Holder
      {
          public int PosX { get; set; }
          public int PosY { get; set; }
  
          public BaseClass Content { get; set; }
      }
  
  
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public List&lt;Condition&gt; Conditions { get; set; }
          public List&lt;Holder&gt; Holders { get; set; }
  
          public int SelectedConditionId { get; set; }
  
          private void GenerateData()
          {
              Conditions = new List&lt;Condition&gt;();
              Conditions.Add(new Condition() { Id = 1, Name = "Condition 1" });
              Conditions.Add(new Condition() { Id = 2, Name = "Condition 2" });
              Conditions.Add(new Condition() { Id = 3, Name = "Condition 3" });
              Conditions.Add(new Condition() { Id = 4, Name = "Condition 4" });
              Conditions.Add(new Condition() { Id = 5, Name = "Condition 5" });
  
              Holders = new List&lt;Holder&gt;();
              Holders.Add(new Holder() { PosX = 10, PosY = 10, Content = new TagClass() { } });
              Holders.Add(new Holder() { PosX = 10, PosY = 110, Content = GenerateConnectionClass(1, Conditions) });
              Holders.Add(new Holder() { PosX = 10, PosY = 210, Content = new TagClass() { } });
              Holders.Add(new Holder() { PosX = 10, PosY = 310, Content = GenerateConnectionClass(2, Conditions) });
              Holders.Add(new Holder() { PosX = 10, PosY = 410, Content = new TagClass() { } });
          }
  
          private ConnectionClass GenerateConnectionClass(int index, List&lt;Condition&gt; conditions)
          {
              var conn = new ConnectionClass();
  
              conn.Name = "Connection " + index;
              conn.CalcData = new List&lt;CalcData&gt;();
              foreach (var c in conditions)
              {
                  conn.CalcData.Add(new CalcData() { Condition = c, Name = "Calc values for condition " + c.Id, Diameter = c.Id * 100 });
              }
  
              return conn;
          }
  
          private CalcData GetCalcDataForCondition(ConnectionClass conn, Condition condition)
          {
              return conn.CalcData.First(c =&gt; c.Condition.Id == condition.Id);
          }
  
          public MainWindow()
          {
              GenerateData();
              SelectedConditionId = 1;
              InitializeComponent();
  
              DataContext = this;
          }
      }
  }
  </code></pre>
  
  <p>MainWindow.xaml</p>
  
  <pre><code>&lt;Window x:Class="FilterSample.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:FilterSample"
          mc:Ignorable="d"
          Title="MainWindow" Height="350" Width="525"&gt;
  
      &lt;Window.Resources&gt;
          &lt;DataTemplate DataType="{x:Type local:TagClass }" &gt;
              &lt;Border BorderBrush="Black" BorderThickness="1"&gt;
                  &lt;StackPanel Orientation="Horizontal" &gt;
                      &lt;Label Content="Tag Class" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
          &lt;DataTemplate DataType="{x:Type local:ConnectionClass }"&gt;
              &lt;Border CornerRadius="4" Background="White" BorderThickness="2" BorderBrush="{DynamicResource NormalBorderBrush}" &gt;
                  &lt;StackPanel Orientation="Vertical"&gt;
                      &lt;Label Content="Connection Class" /&gt;
  
                      &lt;Label Content="HERE I WANT TO DISPLAY related CalcData.Name" /&gt;
                  &lt;/StackPanel&gt;
              &lt;/Border&gt;
          &lt;/DataTemplate&gt;
  
          &lt;DataTemplate DataType="{x:Type local:Holder }"&gt;
              &lt;StackPanel Orientation="Horizontal"  &gt;
                  &lt;Label Content="This is Holder Class" FontWeight="Bold" /&gt;
                  &lt;ContentControl Content="{Binding Path=Content}" /&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
  
      &lt;/Window.Resources&gt;
  
      &lt;Grid&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="50" /&gt;
              &lt;RowDefinition Height="*" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;StackPanel Orientation="Horizontal"&gt;
              &lt;Label Content="Select condition " Height="24" /&gt;
              &lt;ComboBox ItemsSource="{Binding Conditions}" SelectedValue="{Binding SelectedConditionId}" DisplayMemberPath="Name" SelectedValuePath="Id" Width="200" Height="24" /&gt;
  
          &lt;/StackPanel&gt;
  
          &lt;ItemsControl ItemsSource="{Binding Holders}" Grid.Row="1"&gt;
              &lt;ItemsControl.ItemsPanel&gt;
                  &lt;ItemsPanelTemplate&gt;
                      &lt;Canvas /&gt;
                  &lt;/ItemsPanelTemplate&gt;
              &lt;/ItemsControl.ItemsPanel&gt;
              &lt;ItemsControl.ItemContainerStyle&gt;
                  &lt;Style TargetType="ContentPresenter"&gt;
                      &lt;Setter Property="Canvas.Left" Value="{Binding Path=PosX , Mode=OneWay}" /&gt;
                      &lt;Setter Property="Canvas.Top" Value="{Binding Path=PosY, Mode=OneWay}" /&gt;
                  &lt;/Style&gt;
              &lt;/ItemsControl.ItemContainerStyle&gt;
          &lt;/ItemsControl&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>