Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding ComboBox in ListView in MVVM following Josh Smith's Article
    primarykey
    data
    text
    <p>I have recently started developing WPF Applications using MVVM. I am following <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow">Josh Smith's article</a>. I have written a basic screen which simply displays the data in a ListView from the ViewModel's ObservableCollection class. It's a <code>List&lt;string&gt;</code>. I just simply want to be able to display the List of strings and than be able to save it back in underlying Property of Parent collection. </p> <p>Should I be using RelativeSource to link with ViewModel as DataContext? </p> <p>Here's ViewModel Code. </p> <pre class="lang-cs prettyprint-override"><code>public class ViewModel { public List&lt;string&gt; AndOrList { get { List&lt;string&gt; andOrList = new List&lt;string&gt; { "AND", "OR" }; return andOrList; } } public ObservableCollection&lt;BusObjects.Criterion&gt; Criteria { get { return new ObservableCollection&lt;BusObjects.Criterion&gt;(_stream.ParseFilterCriteria().Criterias); } } } </code></pre> <p>Here's the problem Area</p> <pre><code>&lt;GridViewColumn Header="And / Or"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding AndOrList}"&gt; &lt;/ComboBox&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; </code></pre> <p>Here's the whole XAML Code. I am trying to bind the AndOrList collection to ComboBox. </p> <pre><code>&lt;UserControl.Resources&gt; &lt;local:FilterCriteriaConverter x:Key="FilterCriteriaConverter"/&gt; &lt;CollectionViewSource x:Key="CriteriaList" Source="{Binding Path=Criteria}"&gt; &lt;CollectionViewSource.SortDescriptions&gt; &lt;scm:SortDescription PropertyName="DimName" Direction="Ascending"/&gt; &lt;/CollectionViewSource.SortDescriptions&gt; &lt;/CollectionViewSource&gt; &lt;Style x:Key="CriteriaItemStyle" TargetType="{x:Type ListViewItem}"&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch" /&gt; &lt;Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}" /&gt; &lt;Style.Triggers&gt; &lt;MultiTrigger&gt; &lt;MultiTrigger.Conditions&gt; &lt;Condition Property="ItemsControl.AlternationIndex" Value="1" /&gt; &lt;Condition Property="IsSelected" Value="False" /&gt; &lt;Condition Property="IsMouseOver" Value="False" /&gt; &lt;/MultiTrigger.Conditions&gt; &lt;Setter Property="Background" Value="#EEEEEEEE" /&gt; &lt;/MultiTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; &lt;/UserControl.Resources&gt; &lt;DockPanel&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="5"/&gt; &lt;RowDefinition Height="*"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;ListView Name="lstCriteria" Grid.Row="0" AlternationCount="2" DataContext="{StaticResource CriteriaList}" ItemContainerStyle="{StaticResource CriteriaItemStyle}" ItemsSource="{Binding}" DockPanel.Dock="Top"&gt; &lt;ListView.Resources&gt; &lt;Style TargetType="ListViewItem"&gt; &lt;Setter Property="HorizontalContentAlignment" Value="Stretch"/&gt; &lt;/Style&gt; &lt;/ListView.Resources&gt; &lt;ListView.View&gt; &lt;GridView&gt; &lt;GridViewColumn&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;Rectangle Fill="SteelBlue" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="And / Or"&gt; &lt;GridViewColumn.CellTemplate&gt; &lt;DataTemplate&gt; &lt;ComboBox ItemsSource="{Binding AndOrList}"&gt; &lt;/ComboBox&gt; &lt;/DataTemplate&gt; &lt;/GridViewColumn.CellTemplate&gt; &lt;/GridViewColumn&gt; &lt;GridViewColumn Header="Name" DisplayMemberBinding="{Binding Path=DimName}" /&gt; &lt;GridViewColumn Header="Oprator" DisplayMemberBinding="{Binding Path=Operator}" /&gt; &lt;GridViewColumn Header="Value" DisplayMemberBinding="{Binding Path=DimValue}" /&gt; &lt;/GridView&gt; &lt;/ListView.View&gt; &lt;/ListView&gt; &lt;Border Grid.Row="1" CornerRadius="4" BorderThickness="1"&gt; &lt;GridSplitter HorizontalAlignment="Stretch" VerticalAlignment="Center" Height="3" Background="SteelBlue"/&gt; &lt;/Border&gt; &lt;Border Name="xmlBorder" Grid.Row="2" VerticalAlignment="Stretch" CornerRadius="4" BorderThickness="1" &gt; &lt;StackPanel x:Name="stkPanel" Grid.Row="2" Height="Auto" VerticalAlignment="Stretch" &gt; &lt;XMLViewer:Viewer x:Name="xmlViewer" VerticalAlignment="Stretch" /&gt; &lt;TextBox x:Name="txtXml" TextChanged="XmlTextChanged" Visibility="Hidden" Text="{Binding Path=FilterCriteria, Converter={StaticResource FilterCriteriaConverter}}" TextWrapping="Wrap"/&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/Grid&gt; &lt;/DockPanel&gt; </code></pre> <p>Thanks</p> <p>Pak</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.
 

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