I am writing a C# WPF application and following the MVVM pattern.
My
ViewModel
has an object in it called
NetworkMain
My
View
has a
DataGrid
which binds to an
ObservableCollection
called
FlowControls
which is inside
NetworkMain
My
DataGrid
Itemsource is defined as follows:
<DataGrid ItemsSource="{Binding Path=NetworkMain.FlowControls}">
The above is working fine.
I want one of my columns in the
DataGrid
to be a
ComboBox
.
My column is defined as shown in the code at the bottom of this question.
The problem I have is I want the source of the
ComboBox
to use a collection called
Pipes
which is in
NetworkMain
and not
FlowControls
.
How can I do this?
I tried using
Binding Path=NetworkMain.Pipes
but I think it is looking for
NetworkMain
from within
FlowControls
which isn't correct.
Hope the above makes sense.
What I have tried:
<DataGridTemplateColumn Header="Downstream Pipe ID">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox
SelectedItem="{Binding Path=SelectedDownstreamPipeID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding Path=NetworkMain.Pipes}"
DisplayMemberPath="Description" >
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
EDIT
I have updated the XAML as follows and I now get the list I want in the
ComboBox
. What I can't get to work is the setting of the selected item.
I get the following error:
System.Windows.Data Error: 1 : Cannot create default converter to perform 'two-way' conversions between types 'Drain.Models.Network.Conduit' and 'System.String'. Consider using Converter property of Binding.
<UserControl.Resources>
<CollectionViewSource
x:Key="Conduits" Source="{Binding NetworkMain.Conduits}">
</CollectionViewSource>
</UserControl.Resources>
<DataGridComboBoxColumn
Header="Downstream Pipe ID"
SelectedItemBinding="{Binding outgoingConduit, Mode=OneWayToSource}"
ItemsSource="{Binding Source={StaticResource Conduits}}"
DisplayMemberPath="ID"/>
public ObservableCollection<Conduit> Conduits { get; set; } = new();
public Conduit outgoingConduit
{
get;
set;
}
I tried adding the following convertor:
<conv:ConduitObjectConvertor x:Key="ConduitObjectConvertor" />
SelectedItemBinding="{Binding outgoingConduit, Mode=TwoWay, Converter={StaticResource ConduitObjectConvertor}}"
I copied and pasted a convertor from an example online and I thought I adapted it correctly but I still get errors. The convertor is here:
public class ConduitObjectConvertor : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context,
Type sourceType)
{
return sourceType == typeof(Conduit);
}
public override object ConvertFrom(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value)
{
return value as Conduit;
}
public override bool CanConvertTo(ITypeDescriptorContext context,
Type destinationType)
{
return destinationType == typeof(Conduit);
}
public override object ConvertTo(ITypeDescriptorContext context,
System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return value == null ? null : value as Conduit;
}
}
The error I get is:
InvalidCastException: Unable to cast object of type 'Drain.Helper.Convertors.ConduitObjectConvertor' to type 'System.Windows.Data.IValueConverter'.