ListView and TreeView - share same grid row but us

2019-08-22 10:45发布

It's me again.

I felt like this was worth a separate question, since I might've not known what I was exactly trying to ask for previous. Let me visually break down the problem. I have a view like this:

enter image description here

This looks OK, but as you can see, the "Issues" (ListView) section has children that populate for each of the children on the TreeView on the left.

What I'm trying to achieve is to have the ability to expand a parent node of the TreeView (with the datetime string) so that the ListView side, for one, would increase the length of the corresponding child the length that the TreeView's child expanded. I'm also trying to ensure that they are visually on the same line (granted, it would help to resize the children in the TreeView and perhaps reduce the border of the "Issues" box).

Is this possible? Is a possible implementation of this more complicated than not?

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>

Here's my 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.");
        }
    }
}

Forgive me if this is technically more of a repeat question compared to my previous one. I just needed to be sure that my issue was clear.

0条回答
登录 后发表回答