Print Page | Close Window

Hierarachical Related Binding

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2456
Printed Date: 03-Sep-2025 at 10:06am


Topic: Hierarachical Related Binding
Posted By: quest202
Subject: Hierarachical Related Binding
Date Posted: 20-Jan-2011 at 5:57pm

I have an Incident Report data form which also lists all possible threat types for user to choose for this Incident Report.  The threat types are in Threat table which is hierarchical categorized with parent-child relationship.

So I have 3 tables.

The Incident Report table consists of fields: ReportID (primary,unique), Title, Story….

The Threat table consists of fields: ThreatID (primary,unique),  ParentThreatID, Name ….

The third table tied the above 2 tables together.  I called it the Mapping table.  The Mapping table consists of fields: MappingID (primary,unique), ReportID, and ThreatID.  The purpose of the Mapping table is to keep track of the threat types assigned to a report.

In my ViewModel, I have the following in the constructor:

Reports = new ObservableCollection<Report>();

var query = _mgr.Reports.Include("Mappings");

query.ExecuteAsync(op => op.Results.ForEach(this.Reports.Add));

 
I am able to display the Incident Report DataForm and all possible Threat types from the Threat table in a tree view (HierachicalDataTemplate), and I put checkboxes in front of the tree item.  My problem is now I need to check the box of the appropriate Threat if the ReportID existed in the Mapping table.  Furthermore, if the ReportID for the Threat does not exist in the Mapping table, I need to add a new record to the Mapping table to keep track of it.

I am not sure how to get to the Report’s related entities of the Mapping records, and assigned them to the checkboxes. 

<sdk:TreeView x:Name="treeThreats" itemsSource="{Binding Threats,Source={StaticResource vm}}" >

        <sdk:TreeView.ItemTemplate>

               <winControls:HierarchicalDataTemplate ItemsSource="{Binding ChildThreats}" >

                    <StackPanel Orientation="Horizontal">

                            <CheckBox x:Name="chkThreat" IsChecked="{Binding Mappings, Source={StaticResource vm}}" />

                             <TextBlock Text="{Binding Name, Mode=OneWay}" Margin="5,0" />

                    </StackPanel>

                </winControls:HierarchicalDataTemplate>

         </sdk:TreeView.ItemTemplate>

</sdk:TreeView>

 

Thank you for your help




Replies:
Posted By: DenisK
Date Posted: 21-Jan-2011 at 12:54pm
Hi quest202;

The CheckBox IsChecked property is a boolean and as such it needs to be binded to a boolean property as well.

To get the Report's related entities of the Mapping records, you can do a logic similar to the following:

//Note that this works because you have .Include("Mappings") in your query. This line will throw an exception if you //haven't.
var mappings = Reports[0].Mappings;

Then you can do some business logic with the mappings and assign a public boolean property that will be binded to the CheckBox IsChecked property. For example:

View:

<CheckBox x:Name="chkThreat" IsChecked="{Binding IsChecked}" />

ViewModel:

public bool IsChecked {
get {
return _isChecked;
}
set {
_isChecked = value;
RaisePropertyChanged("IsChecked");
}
}

private bool _isChecked;

private void DoSomeLogicWithMappings(object[] mappings) {
..............
_isChecked = true;
................
}

Your implementation can be very much different from this. I'm just illustrating how to get the related entities and how to bind to the CheckBox IsChecked property.

Hope this helps.



Print Page | Close Window