1
votes

I have a combobox and a datagrid in my application. The datagrid has its itemsSource from its collectionViewSource and there are three ComboBoxItem's in the combobox as warning/error/exception as shown in the image below. enter image description here

How to display the selecteditem row details on the datagrid when the respective ComboxBoxitem is selected.

this what I have tried. Combobox - XAML

<ComboBox 
SelectedValuePath="{Binding ElementName=dataGrid1,Path=SelectedItem.Type,Mode=OneWay}"
Grid.Column="1" Height="32" HorizontalAlignment="Left" Name="comboBox1" >
<ComboBoxItem Content="Warning"/>
<ComboBoxItem Content="Error"/>
<ComboBoxItem Content="Exception"/>
</ComboBox>

datagrid's XAML

<DataGrid AutoGenerateColumns="False" 
IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Name="dataGrid1">

is it possible to achieve this via XAML skipping code behind ? if not other suggestions are also most welcome.

2
is there a occasion to deny code behind ? Maybe you cloud write your code in your XAML using CDATA but i doesn't know how to use this and i would recommend to use normal code like Raul Otaño does it in his exampleWiiMaxx
no not really. to avoid code behind is just an option.. but if there is a way to implement in code behind I can use it..Indhi
do you use any pattern e.g. mvvm?WiiMaxx
No.. I am using entity framework..but to explain- My Model is my EF, View and my ViewModel is just the code behind ( using entites to access my model)Indhi

2 Answers

2
votes

This is a code sample that may helps you. It shows a Collection View Source, with a filter...

XAML

    <Window x:Class="Ejemplos_EnlaceADatos.Figura5_12"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cmod="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:cine="clr-namespace:Ejemplos_EnlaceADatos.Cine"
    Title="Lista de Films" Height="200" Width="300"
>
  <Window.Resources>
    <CollectionViewSource x:Key="films" Source="{x:Static cine:Filmes.Films}" Filter="Filter_Film">
      <CollectionViewSource.SortDescriptions>
        <cmod:SortDescription PropertyName="Título"/>
      </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
  </Window.Resources>
  <ScrollViewer>
    <StackPanel TextBlock.FontFamily="Segoe UI" Margin="6">
      <TextBlock FontSize="16" FontWeight="Bold" Foreground="Navy">
        Films:
      </TextBlock>
      <ItemsControl ItemsSource="{Binding Source={StaticResource films}}"/>
    </StackPanel>
  </ScrollViewer>
</Window>

Code Behind

using System.Windows;
using System.Windows.Data;
using Ejemplos_EnlaceADatos.Cine;

namespace Ejemplos_EnlaceADatos {
  public partial class Figura5_12 : Window {

    public Figura5_12() {
      InitializeComponent();
    }
    void Filter_Film(object sender, FilterEventArgs e) {
      e.Accepted = (e.Item is Film) && (((Film)e.Item).Género == Género.Mafia);
    }
  }
}

And filter like this:

Before

enter image description here

After

enter image description here

It is just an example of filtering, but is it with a regular items control.

0
votes

You can make use of DataGrid's Filter,
Refer here for more details : http://msdn.microsoft.com/en-us/library/ff407126.aspx