Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Working with the new TreeView and CheckBox - System.Windows.Controls - Problem...

$
0
0
Hello!
After a massive search online and here, I came to the conclusion that I must ask a question here, otherwise I'll never find an answer (:

I am writing a program in C # and WPF, and I built a tree folders.
I managed to do the tree with the class of DOT NET 4.5 => System.Windows.Controls
But I do not know how to add CHECKBOX beside each folder tree, and when I click the CHECKBOX it will return me the path of the selected folder.

This screenshot of the folder tree:




Unfortunately there is no more possibility class System.Windows.Controls change the "Property" to IsCheckBox = true, because it is no longer using System.Windows.Forms

This is my code behind:

    private object LocalDummyNode = null;
        public string SelectedLocalPath { get; set; }
        private bool TreeViewIsOpened = false;
        private void LocalFoldersItem_Loaded(object sender, RoutedEventArgs e)
        {
            if (TreeViewIsOpened != true)
            {
                foreach (var drive in DriveInfo.GetDrives())
                {
                    TreeViewItem item = new TreeViewItem();
                    item.Header = drive;
                    item.Tag = drive;
                    item.FontWeight = FontWeights.Normal;
                    item.Items.Add(LocalDummyNode);
                    item.Expanded += new RoutedEventHandler(LocalFolder_Expanded);
                    LocalFoldersItem.Items.Add(item);
                }
            }
            
            TreeViewIsOpened = true;

        }

        void LocalFolder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = (TreeViewItem)sender;
            if (item.Items.Count == 1 && item.Items[0] == LocalDummyNode)
            {
                item.Items.Clear();
                try
                {
                    foreach (string s in Directory.GetDirectories(item.Tag.ToString()))
                    {
                        TreeViewItem subitem = new TreeViewItem();
                        subitem.Header = s.Substring(s.LastIndexOf("\\") + 1);
                        subitem.Tag = s;
                        subitem.FontWeight = FontWeights.Normal;
                        subitem.Items.Add(LocalDummyNode);
                        subitem.Expanded += new RoutedEventHandler(LocalFolder_Expanded);
                        item.Items.Add(subitem);
                    }
                }
                catch (Exception) { }
            }
        }

        private void LocalFoldersItem_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            TreeView tree = (TreeView)sender;
            TreeViewItem temp = ((TreeViewItem)tree.SelectedItem);

            if (temp == null)
                return;
            SelectedLocalPath = "";
            string temp1 = "";
            string temp2 = "";
            while (true)
            {
                temp1 = temp.Header.ToString();
                if (temp1.Contains(@"\"))
                {
                    temp2 = "";
                }
                SelectedLocalPath = temp1 + temp2 + SelectedLocalPath;
                if (temp.Parent.GetType().Equals(typeof(TreeView)))
                {
                    break;
                }
                temp = ((TreeViewItem)temp.Parent);
                temp2 = @"\";
            }
            //show user selected path
            view = new View(); 
            LocallbShowFiles.Items.Clear();
            foreach (var item in view.ShowFiles(SelectedLocalPath))
            {

                LocallbShowFiles.Items.Add(item);
            }
        }

and this is my WPF code:

    <Grid>

                    <TreeView x:Name="LocalFoldersItem" SelectedItemChanged="LocalFoldersItem_SelectedItemChanged" Background="#FFFFFFFF" BorderBrush="#FFFFFFFF" Foreground="#FFFFFFFF" Loaded="LocalFoldersItem_Loaded" Margin="10,53,0,146" HorizontalAlignment="Left" Width="240">
                        <TreeView.Resources>
                            <Style TargetType="{x:Type TreeViewItem}">
                                <Setter Property="HeaderTemplate">
                                    <Setter.Value>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding}" Margin="5,0" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </TreeView.Resources>
                    </TreeView>
                    <ListBox x:Name="LocallbShowFiles" Margin="10,255,0,0" HorizontalAlignment="Left" Width="240"/>
                    <Label x:Name="lblSourceOfCopy" Content="Source (Copy From):" HorizontalAlignment="Left" Margin="10,22,0,0" VerticalAlignment="Top" Width="304"/>

                </Grid>

I know I can change and add the WPF code-line: 

    <StackPanel Orientation="Horizontal">
                                                <CheckBox></CheckBox>
                                                <TextBlock Text="{Binding}" Margin="5,0" />
                                            </StackPanel>

And it looks like this:



But the question is: How can I using the CHECKBOX get the path of the selected folder?

Hope you help me find a solution (:
Thank you.

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>