Hello I have been facing issue to DataGridComboBoxColumn in my simple project can anybody tell me where it went wrong.
I have been working on it since 2 days but couldn’t get any proper solution. I have also not implemented any MVVM model to avoid complexity in learning.
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication10
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var mainViewModel = new MainViewModel();
DataContext = mainViewModel;
//MainViewModel viewModel = new MainViewModel();
//MyGrid.DataContext = viewModel;
}
}
public class Item
{
public int ID { get; set; }
public string Name { get; set; }
public string Code { get; set; }
// Property for the composite value
public string CombinedValue => $"{ID}_{Name}_{Code}";
}
public class MainViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private Item _selectedItem;
public Item SelectedItem
{
get { return _selectedItem; }
set
{
if (_selectedItem != value)
{
_selectedItem = value;
OnPropertyChanged(nameof(SelectedItem));
}
}
}
public ObservableCollection<Item> Items { get; set; }
public ObservableCollection<string> Codes { get; set; }
public MainViewModel()
{
Items = new ObservableCollection<Item>
{
new Item { ID = 1, Name = "Item 1", Code = "A" },
new Item { ID = 2, Name = "Item 2", Code = "B" },
new Item { ID = 3, Name = "Item 3", Code = "C" }
};
Codes = new ObservableCollection<string>(Items.Select(item => item.Code).Distinct());
SelectedItem = Items.FirstOrDefault();
}
}
}
and here is the xaml code for it
<Window x:Class="WpfApplication10.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:WpfApplication10"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525"
>
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<DataGrid x:Name="MyGrid" AutoGenerateColumns="False" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" IsReadOnly="True">
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridComboBoxColumn Header="Code combo"
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:MainViewModel}}, Path=Codes}"
TextBinding="{Binding Code}"
IsReadOnly="False"/>
<DataGridTemplateColumn Header="Temp Code" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate >
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.Codes}"
SelectedItem="{Binding Code}" IsReadOnly="True" IsEnabled="False" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<!--<DataGrid ItemsSource="{Binding Items}">
</DataGrid>-->
<ComboBox ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}"
DisplayMemberPath="CombinedValue"
Width="200"
Height="30" />
</Grid>
</Window>
I am trying to import code values in the list mapped to ID of the data.
The values and all other classes are defined in one document only to get clarity at first about binding.
I am also confused in usage of selectedValueBinding, SelectedItemBinding, SelectedTextBinding and SelectedValue.
Pushkar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.