Binding different lists to different rows in DataGrid
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hello,
I know this question is more WPF oriented but I am still posting it in this forum.
I am not using MVVM yet and am trying to bind different list to different row in DataGrid.
I have 2 lists:
private List<string> roundProfileTypeNames;
private List<string> rectProfileTypeNames;
and I have a DataGrid as follows:
Each row contains a profile name + a checkbox which is checked if profile is rounded (taken from a parameter in the profile type) + a list of profile types which should be taken from the 2 lists above:
If "Is Rounded Profile" is checked then "Profile Type" should be binded to roundProfileTypeNames list, otherwise (unchecked) it should be binded to rectProfileTypeNames.
Currently, I was only able to bind all rows to the same list (roundProfileTypeNames OR rectProfileTypeNames).
This is my datagrid in xaml file:
<DataGrid Name="profilePropsGrid" Width="500" CanUserReorderColumns="False" HorizontalAlignment="Center" CanUserAddRows="False" VerticalAlignment="Top" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Profile Name" Width="*" Binding="{Binding ProfileName, Mode=TwoWay}" IsReadOnly="true" />
<DataGridTemplateColumn Header="Is Rounded Profile?" Width="*">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsRound}" Checked="IsRoundChecked" Unchecked="IsRoundUnchecked" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridComboBoxColumn Width="100" Header="Profile Type" x:Name="ProfileType"
SelectedValueBinding="{Binding ProfileType, Mode=TwoWay}"
DisplayMemberPath="{Binding ProfileType}" />
</DataGrid.Columns>
</DataGrid>
And this is a snippet from my xaml.cs file:
bridgeProfileList = new List<ProfileItem>();
foreach (Element elem in framingPipeTypesCol) // Iterating through all framing pipe types in Revit
{
ProfileItem ep = new ProfileItem()
{
ProfileName = elem.Name,
ProfileElement = elem
};
IList<Parameter> pipe_shape_circle = elem.GetParameters("Pipe Shape_Circle");
if (pipe_shape_circle.Count > 0)
{
int pipe_shape = pipe_shape_circle[0].AsInteger();
if (pipe_shape == 0)
{
ep.IsRound = false;
//ProfileType.ItemsSource = rectProfileTypeNames;
}
if (pipe_shape == 1)
{
ep.IsRound = true;
//ProfileType.ItemsSource = roundProfileTypeNames;
}
}
bridgeProfileList.Add(ep);
Debug.WriteLine(elem.Name);
}
profilePropsGrid.ItemsSource = bridgeProfileList;
ProfileType.ItemsSource = roundProfileTypeNames;
How can I bind the correct list if checkbox is checked/unchecked?
Thank you!