Binding different lists to different rows in DataGrid

Binding different lists to different rows in DataGrid

mizrachi_amir2
Advocate Advocate
910 Views
2 Replies
Message 1 of 3

Binding different lists to different rows in DataGrid

mizrachi_amir2
Advocate
Advocate

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:

mizrahi_amir_0-1713680617016.png

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!

0 Likes
911 Views
2 Replies
Replies (2)
Message 2 of 3

jeremy_tammik
Alumni
Alumni

I don't know WPF myself, so I asked Meta AI for you. It replies:

 

To achieve this, you can create a property in your ProfileItem class that returns the correct list based on the IsRound property. Here's an example:
In your ProfileItem class, add a property like this:
C#
 
public List<string> ProfileTypeList
{
    get { return IsRound ? roundProfileTypeNames : rectProfileTypeNames; }
}
Then, in your XAML, bind the ItemsSource of the DataGridComboBoxColumn to this new property:
Xaml
 
<DataGridComboBoxColumn Width="100" Header="Profile Type" x:Name="ProfileType" 
    SelectedValueBinding="{Binding ProfileType, Mode=TwoWay}"  
    ItemsSource="{Binding ProfileTypeList}" 
    DisplayMemberPath="{Binding ProfileType}" />
This way, when the IsRound property changes, the ProfileTypeList property will return the correct list, and the DataGridComboBoxColumn will update its items accordingly.
Note that I assume roundProfileTypeNames and rectProfileTypeNames are already defined and populated in your code. If not, you'll need to make sure they are properly initialized and accessible.
  
Does that help?
  
Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes
Message 3 of 3

mizrachi_amir2
Advocate
Advocate

I did the same, here is how my ProfileItem class looks like (there are many rows below of initializing the 2 lists):

internal class ProfileItem
{
    public string ProfileName { get; set; }
    public bool IsRound { get; set; }
    public string ProfileType { get; set; }
    public Element ProfileElement { get; set; }

    public List<string> ProfileTypeList
    {
        get { return IsRound ? roundProfileTypeNames : rectProfileTypeNames; }
    }


    public List<string> rectProfileTypeNames = new List<string>();
    public List<string> roundProfileTypeNames = new List<string>();

    public ProfileItem(string name, Element elem)
    {

        this.ProfileName = name;
        this.ProfileElement = elem;
        rectProfileTypeNames.Add("RHS 50x30 / 2.6");
        rectProfileTypeNames.Add("RHS 50x30 / 3.2");
        rectProfileTypeNames.Add("RHS 50x30 / 4");
        rectProfileTypeNames.Add("RHS 50x30 / 5");
   ...
   ...
   }
}

  

I also add ItemsSource="{Binding ProfileTypeList}" to my 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}" IsReadOnly="true" />
        <DataGridTemplateColumn Header="Is Rounded Profile?" Width="*" >
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Path=IsRound}" Checked="IsRoundChecked" Unchecked="IsRoundUnchecked" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridComboBoxColumn Width="100" Header="Profile Type" x:Name="ProfileType" 
             SelectedValueBinding="{Binding Path=ProfileType}"  
             ItemsSource="{Binding ProfileTypeList}"
            DisplayMemberPath="{Binding ProfileType}" />
    </DataGrid.Columns>
</DataGrid>

 

But now my ProfileType column shows me empty lists in all DataGrid rows:

mizrahi_amir_0-1713696060984.png

I can see in my ProfileItem class that ProfileTypeList shows "0 references":

mizrahi_amir_1-1713696121269.png

 

I think that the binding doesn't take place. Isn't it?

0 Likes