0

The latest in my endeavors - I've managed to populate a treeview with elements, and populate a listbox next to it with related elements.

The trick is that, I'm trying to think of a way to assign each listbox item and corresponding treeview item it's own grid row, that would be generated probably in back C# code to with corresponding row definitions based on the control's number of children, if that makes any sense.

I've tried looking at the post below to see if I can select each treeview item this way, but to no avail:

How to programmatically select an item in a WPF TreeView?

Here's some example code that tries to implement this - I keep getting null values, though, and using UpdateLayout() before the loop on the controls doesn't seem to help. :/

                for (int i = 0; i < loadedAR.Item2.Count; i++)
                {
                    DABsAndIssuesGrid.RowDefinitions.Add(new RowDefinition());
                    var selectedItemObjectDAB = DABView.Items.GetItemAt(i);
                    var DABParent = DABView.Parent;
                    TreeViewItem currentItemDAB = DABView.ItemContainerGenerator.ContainerFromItem(selectedItemObjectDAB)
                    as TreeViewItem;
                    if (currentItemDAB != null)
                        Grid.SetRow(currentItemDAB, i);
                    var selectedItemObjectErr = IssueBox.Items.GetItemAt(i);
                    TreeViewItem currentItemErr = DABView.ItemContainerGenerator.ContainerFromItem(selectedItemObjectErr)
                    as TreeViewItem;
                    if (currentItemErr != null)
                        Grid.SetRow(currentItemErr, i);
                }

Is there a better way to do this than the way I'm trying to do it? I've tried using a converter to assign the row dynamically too as they would get generated, but that still appears to put all of my children in the same first grid row...

Here's my XAML:

<Window x:Class="Client_Invoice_Auditor.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:Client_Invoice_Auditor"
        xmlns:self="clr-namespace:Client_Invoice_Auditor.CoreClient"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="1000">
    <Window.Resources>
        <self:SelfPayConverter x:Key="FINConverter"/>
        <self:ErrorExpandConverter x:Key="ErrorExpandConverter"/>
        <self:AssignRowConverter x:Key="AssignRowConverter"/>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>

        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="20*"/>
            <RowDefinition Height="80*"/>
        </Grid.RowDefinitions>
        <Grid Grid.Row="0" Grid.Column="0">
            <StackPanel Orientation="Vertical">
                <DockPanel VerticalAlignment="Top" Height="20" Panel.ZIndex="1">
                    <Menu Name="fileMenu" Width="Auto" DockPanel.Dock="Top">
                        <MenuItem Header="File">
                            <MenuItem Header="Open Account File" Click="menuOpenFile_Click"/>

                            <MenuItem Header="Exit" Click="menuExit_Click"/>
                        </MenuItem>
                        <MenuItem Header="Options">
                            <!--<MenuItem Header="Update" Click="update_Click"/>-->
                            <MenuItem Header="About" Click="about_Click"/>
                        </MenuItem>
                    </Menu>
                </DockPanel>
                <WrapPanel Orientation="Horizontal" HorizontalAlignment="Center" Height="Auto">
                    <StackPanel Width="Auto" Orientation="Horizontal" HorizontalAlignment="Center">
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="AccountNumber"/>
                        </Border>
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="AcctDesc"/>
                        </Border>
                        <Border BorderBrush="MediumAquamarine" BorderThickness="2">
                            <Label Name="Organization"/>
                        </Border>
                    </StackPanel>
                </WrapPanel>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                    <Label Margin="20,10,0,0" Content="Activity Date Time" />
                    <Label Margin="60,10,0,0" Content="Beginning Balance" />
                    <Label Margin="10,10,0,0" Content="Charge Amount" />
                    <Label Margin="30,10,0,0" Content="Adjustments" />
                    <Label Margin="40,10,0,0" Content="Payments" />
                    <Label Margin="60,10,0,0" Content="End Balance" />
                    <Label Margin="50,10,0,0" Content="Issues" />
                </StackPanel>
            </StackPanel>

        </Grid>
        <Grid Grid.Row="1" Grid.Column="0">
            <Grid Name="DABsAndIssuesGrid">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="80*"/>
                    <ColumnDefinition Width="20*"/>
                </Grid.ColumnDefinitions>
                <TreeView Name="DABView" Grid.Column="0">
                    <!--<Style TargetType="{x:Type TreeViewItem}">
                        <Setter Property="IsExpanded" 
                                                Value="{Binding FinClass, Converter={StaticResource ErrorExpandConverter}}" />
                    </Style>-->
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="IsExpanded" 
                                                Value="{Binding Dummies, Converter={StaticResource ErrorExpandConverter}}" />
                        </Style>
                    </TreeView.ItemContainerStyle>
                    <TreeView.ItemTemplate>

                        <HierarchicalDataTemplate DataType="{x:Type self:dailyAccountBalance}" ItemsSource="{Binding Dummies}">

                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" IsEnabled="False">
                                    <TextBlock Width="150" Text="{Binding DabActivityDate}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding BegBalance}"/>
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding ChrgAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding AdjAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding PmtAmount}" />
                                    <TextBlock Width="100" Margin="20,0,0,0" Text="{Binding EndBalance}" />
                                </StackPanel>

                            <HierarchicalDataTemplate.ItemTemplate>

                                <DataTemplate DataType="{x:Type self:DummyItem}">

                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                                        <!--<TextBlock Text="{Binding Text}" />-->
                                        <DataGrid ItemsSource="{Binding ChargeActivities}" AutoGenerateColumns="False">

                                            <DataGrid.Style>
                                                <Style TargetType="{x:Type DataGrid}">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Path=Items.Count,
                                                                RelativeSource={RelativeSource Self}}"  Value="0">
                                                            <Setter Property="Visibility" Value="Collapsed" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </DataGrid.Style>

                                            <DataGrid.Columns>
                                                <DataGridTextColumn x:Name="ChrgID"  Header=" Charge" Binding="{Binding ChargeID}" />
                                                <DataGridTextColumn x:Name="ChrgType"  Header="Charge Type" Binding="{Binding ChargeType}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Style.Triggers>
                                                                <Trigger Property="Text" Value="CR">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </Trigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn x:Name="ChrgAmt"  Header="Amount" Binding="{Binding ChargeAmount}">
                                                    <DataGridTextColumn.ElementStyle>

                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Style.Triggers>
                                                                <DataTrigger Binding="{Binding ChargeType}" Value="CR">
                                                                    <Setter Property="Foreground" Value="Red"/>
                                                                </DataTrigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                                <DataGridTextColumn x:Name="FIN"  Header="FIN" Binding="{Binding EncntrAlias}" />
                                                <DataGridTextColumn x:Name="FINClass"  Header="FIN Class" Binding="{Binding FinClass}">
                                                    <DataGridTextColumn.ElementStyle>
                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Setter Property="Foreground" Value="{Binding FinClass, Converter={StaticResource FINConverter}}"/>
                                                        </Style>
                                                    </DataGridTextColumn.ElementStyle>
                                                </DataGridTextColumn>
                                            </DataGrid.Columns>
                                        </DataGrid>
                                    </StackPanel>
                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>

                    </HierarchicalDataTemplate>

                        <!--<TreeViewItem x:Key="Test">
                            <TextBlock Text="Me gusta"></TextBlock>
                        </TreeViewItem>-->
                    </TreeView.ItemTemplate>
                </TreeView>
                <Border Grid.Column="1" BorderBrush="Black" BorderThickness="2">
                    <ListBox Name="IssueBox" ItemsSource="{Binding}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="Hi"/>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </Border>
            </Grid>
        </Grid>
    </Grid>
</Window>

And, here's the code behind:

using Client_Invoice_Auditor.CoreClientAR;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client_Invoice_Auditor
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        public string accountFile;
        public MainWindow()
        {
            InitializeComponent();
            OpenaccountFile();
        }

        private void OpenaccountFile()
        {
            accountFile = "";
            //errorFilters.Clear();
            // Displays an OpenFileDialog so the user can select a file.  
            Microsoft.Win32.OpenFileDialog openFileDialog1 = new Microsoft.Win32.OpenFileDialog();
            openFileDialog1.Filter = "Account Files|*.acct";
            openFileDialog1.Title = "Select an account File";

            if (openFileDialog1.ShowDialog() == true)
            {
                // Assign the cursor in the Stream to the Form's Cursor property.
                accountFile = openFileDialog1.FileName;
                //claimFile = openFileDialog1.OpenFile().ToString();
                //openFileDialog1.Dispose();
            }
            if (accountFile == "")
            {
                /*System.Windows.MessageBox.Show("File must be selected in order to continue - exiting now."
                    , "No File Selected", MessageBoxButton.OK);
                this.Close();*/
                if (!AcctDesc.HasContent)
                {
                    AcctDesc.Content = "No Account File Loaded";
                    //Version version = Assembly.GetExecutingAssembly().GetName().Version;
                    //manualBreakItem.IsEnabled = false;
                    //manualValidateItem.IsEnabled = false;
                }
            }
            else
            {
                //openFileDialog1 = null;
                Console.WriteLine("Account file path is: " + accountFile);
                //claimFile = "C:\\Users\\KO054202\\Documents\\pft_1450_out\\pft_1450_out\\CAR837I125114220170601.out";
                DataTable dataAR = new DataTable();

                try
                {
                    Tuple<accountARHeader, List<dailyAccountBalance>, DataTable> loadedAR = dabARLoader.LoadARData(accountFile);
                    //dataAR = loadedAR.Item2;
                    AccountNumber.Content = "Account Number: " + loadedAR.Item1.AccountNumber;
                    AcctDesc.Content = "Description: " + loadedAR.Item1.AccountDescription;
                    Organization.Content = "Client Organization: " + loadedAR.Item1.OrganizationName;
                    //TreeViewItem dummy = new TreeViewItem();
                    //dummy.DataContext = "Hi";
                    //DummyItem testThis = new DummyItem("Test2");
                    //testThis.Text = "Hi";
                    //loadedAR.Item2.First().Dummies.Add(testThis);
                    DABView.ItemsSource = loadedAR.Item2;
                    IssueBox.ItemsSource = dabARLoader.loadIssues(loadedAR.Item2);
                    DABView.UpdateLayout();
                    for (int i = 0; i < loadedAR.Item2.Count; i++)
                    {
                        DABsAndIssuesGrid.RowDefinitions.Add(new RowDefinition());
                        var selectedItemObjectDAB = DABView.Items.GetItemAt(i);
                        var DABParent = DABView.Parent;
                        TreeViewItem currentItemDAB = DABView.ItemContainerGenerator.ContainerFromItem(selectedItemObjectDAB)
                        as TreeViewItem;
                        if (currentItemDAB != null)
                            Grid.SetRow(currentItemDAB, i);
                        var selectedItemObjectErr = IssueBox.Items.GetItemAt(i);
                        TreeViewItem currentItemErr = DABView.ItemContainerGenerator.ContainerFromItem(selectedItemObjectErr)
                        as TreeViewItem;
                        if (currentItemErr != null)
                            Grid.SetRow(currentItemErr, i);
                    }


                    //DABView.DisplayMemberPath = "A";
                }
                catch (Exception e)
                {
                    System.Windows.MessageBox.Show("I don't wanna open this file! Try another. Error: " + e.Message);
                    OpenaccountFile();
                }

            }
        }

        private void menuOpenFile_Click(object sender, RoutedEventArgs e)
        {
            OpenaccountFile();
        }

        private void menuExit_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void about_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.MessageBox.Show("I heard you like clicking buttons.");
        }
    }
}
Kieran Ojakangas
  • 495
  • 4
  • 18
  • Did you try to bind both controls (TreeView and Grid) to the same List in Backend? So both controls would bind same objects, and a element in the Tree is a specific element in the grid. – Daniel W. Nov 05 '18 at 06:41
  • @KieranOjakangas: There is an example of MSDN of how you can find a TreeViewItem in a TreeView if that's what you are asking about: https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/how-to-find-a-treeviewitem-in-a-treeview – mm8 Nov 05 '18 at 09:11
  • Hey @mm8, thanks for pointing out that article to me, not sure how I hadn't found it before. I will update the post here with more code if needed for sure. I have a TreeView inside of a Grid, along with a ListBox control. The idea is that the TreeView would populate parent items by a daily account balance, and then for each one of those an adjacent list of issues would be displayed in the neighboring ListBox, if that makes sense - hence why I'm trying to have the "issue section" for each daily account balance share the same row in their parent grid... – Kieran Ojakangas Nov 05 '18 at 14:51
  • Hey guys, I provided the full XAML and its code behind. Let me know if you have questions on other classes or anything, but I think this should be more than enough – Kieran Ojakangas Nov 05 '18 at 16:25
  • @DanielW., the TreeView and ListBox are in the same Grid - if that's what you're asking... – Kieran Ojakangas Nov 05 '18 at 22:41

1 Answers1

0

I have a TreeView inside of a Grid, along with a ListBox control. The idea is that the TreeView would populate parent items by a daily account balance, and then for each one of those an adjacent list of issues would be displayed in the neighboring ListBox

I would make specialized Data Transfer Object (DTO)s where each instance item generates the values for the properties to be shown and where required creates child DTOs for that instance. Then those DTOs are bindable between all the controls. Meaning that you have this class defined

<Top Level Class used by the grid<DTO>>
   <Properties et all>
   <List of Items in for treeview>
      <Treeview class <DTO>>
          <Properties et all>
          <List of Items in for listbox>
              <Listbox class <DTO>>
              <Properties et all>
  1. Then bind the grid to the above top level DTO list.
  2. Bind the treeview to the current row's treeview list items.
  3. Bind the listbox to the current treeview's selected item's Listbox items list.

Once the DTOs are created then it simply becomes an exercise in binding.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • Data Transfer Objects? Just for passing data? Care to expand? – Kieran Ojakangas Nov 05 '18 at 20:11
  • An Data Transfer Object (DTO) is a class which will usually mirror a model of data from a database but can also provide extra extended properties with unique business logic; and the DTO is a vector of said original data between different layers in an application. What is being recommended here, is to create a new type of data object which mirrors the original data but *also* understands its location in your hierarchy and has the *sub* data needed to be bound to the specific controls. – ΩmegaMan Nov 05 '18 at 20:16
  • Is there a tutorial out there for this? Or is there a way we can do a paired programming session to accomplish this? I'm trying to release the minimum amount of my code possible but I also want to make sure I can follow your suggestion – Kieran Ojakangas Nov 05 '18 at 22:36
  • Safari Tech books online subscription and pull up WPF books. Pluralsight and bring up WPF/C#. What you are attempting is to create, in essence, a custom control which handles this one situation of some type of dashboard. You understand the broad strokes of WPF programminhg, but you need to learn the subtleties of WPF to bring it all together. – ΩmegaMan Nov 06 '18 at 11:22
  • I was just watching a tutorial on custom controls about five-ten minutes ago. Does your previous comments basically apply to my question here as well? https://stackoverflow.com/questions/53164815/listview-and-treeview-share-same-grid-row-but-use-different-columns – Kieran Ojakangas Nov 08 '18 at 04:20
  • Creation a custom control is good, but but you will still need to organize your data so bindings and ultimately displaying the data is easier and less problematic. – ΩmegaMan Nov 08 '18 at 09:25
  • What you should do is break this down into little components. In a side wpf project create the data which goes in the list view. Bind it and get it running. Then add a treeview which interacts with the listview. Did you need to change the structure in how you present your test data? Most likely. THen when you are happy with the treview + lisbox...add a grid. Did your test data change again? You will learn and build the final control by doing small projects like that. GL – ΩmegaMan Nov 08 '18 at 11:59