I am Working on a Backuptool but now i ran into a problem, all my Items in my ListBox where they were read from an OpenFolderDialog all have checkboxes. Now here is my Problem, I want to get the checked Items into another ListBox called lbSelectedItems from where they should get packed into a rar file after i chose all files i wanna save.
But how can i get the checked Items into my second Listbox and how can they keep the names?
Thats my Code:
public partial class MainWindow : Window
{
private DispatcherTimer timer;
public string ParentPath;
public int RepeatNum = 0;
public int StartLoop = 0;
public bool TimerActive = false;
public MainViewModel Model
{
get => this.DataContext as MainViewModel;
set => this.DataContext = value;
}
public MainWindow()
{
InitializeComponent();
ReloadPath();
FolderBrowserDialog dialog = new FolderBrowserDialog();
DialogResult result = dialog.ShowDialog();
dialog.Dispose();
string selectedPath = dialog.SelectedPath;
Model.TryNavigateToPath(selectedPath);
tbCurrentPath.Text = selectedPath;
if (tbCurrentPath.Text != null)
{
StartLoop = 1;
}
}
private void LoadDirectory(string path)
{
if (StartLoop == 1)
{
while (RepeatNum <= 200)
{
Thread.Sleep(500);
if (tbCurrentPath.Text != null)
{
LoadCurrentPath();
RepeatNum++;
}
}
}
}
public void LoadCurrentPath()
{
tbCurrentPath.Text = Model.CurrentPath;
}
private void btnDirUp_Click(object sender, RoutedEventArgs e)
{
LoadCurrentPath();
}
List<object> Paths = new List<object>();
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
if (LbData.SelectedItem != null)
{
string selectedItem = LbData.SelectedItem.ToString();
lbSelectedItems.Items.Add(selectedItem);
lbSelectedItems.ItemsSource = selectedItem;
}
lbSelectedItems.Items.Add(LbData.SelectedItems);
}
private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
lbSelectedItems.Items.Remove(LbData.SelectedItems);
}
private void tbCurrentPath_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
private void Timeout()
{
btnTestReaction.Visibility = Visibility.Hidden;
Thread.Sleep(500);
btnTestReaction.Visibility = Visibility.Visible;
}
private void ReloadPath()
{
Thread.Sleep(100);
ListBoxItem selectedItem = LbData.SelectedItem as ListBoxItem;
if (selectedItem != null)
{
string selectedPath = selectedItem.Content.ToString();
}
}
private void LbData_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
foreach (object item in e.AddedItems)
{
}
foreach (object item in e.RemovedItems)
{
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ReloadPath();
}
private void LbData_Loaded(object sender, RoutedEventArgs e)
{
}
private void LbData_SourceUpdated(object sender, DataTransferEventArgs e)
{
}
private void btnTestReaction_Click(object sender, RoutedEventArgs e)
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(50);
timer.Tick += timer_Tick;
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
pgTesting.Value +=0.75;
if (pgTesting.Value == pgTesting.Maximum)
{
timer.Stop();
pgTesting.Value = 0;
System.Windows.MessageBox.Show("Test complete!");
}
}
}
}
XAML:
<Window x:Class="FileExplorer.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:FileExplorer"
xmlns:viewmodels="clr-namespace:FileExplorer.ViewModels"
mc:Ignorable="d"
Title="PT_BackupTool"
Icon="favicon.ico"
Height="720" Width="1280"
Style="{DynamicResource CustomWindowStyle}" Loaded="Window_Loaded">
<Window.DataContext>
<viewmodels:MainViewModel/>
</Window.DataContext>
<Grid>
<Grid VerticalAlignment="Top" Height="20" Margin="-1,27,447,0">
<TextBlock Text="Ico" VerticalAlignment="Center" Margin="5,2,0,2" HorizontalAlignment="Left" Width="20"/>
<TextBlock Text="File Name" VerticalAlignment="Center" Margin="29,2,410,2"/>
<TextBlock Text="Date Created" VerticalAlignment="Center" Margin="0,2,283,2" HorizontalAlignment="Right" Width="125" TextAlignment="Center"/>
<TextBlock Text="Date last Modified" VerticalAlignment="Center" Margin="0,2,156,2" HorizontalAlignment="Right" Width="125" TextAlignment="Center"/>
<TextBlock Text="Type" VerticalAlignment="Center" Margin="0,2,84,2" HorizontalAlignment="Right" Width="70" TextAlignment="Center"/>
<TextBlock Text="Size" VerticalAlignment="Center" Margin="0,2,7,2" HorizontalAlignment="Right" Width="75" TextAlignment="Center"/>
</Grid>
<ListBox x:Name="LbData" ItemsSource="{Binding FileItems, UpdateSourceTrigger=PropertyChanged}"
HorizontalContentAlignment="Stretch" Margin="0,47,0,0" HorizontalAlignment="Left" Width="824" SelectionChanged="LbData_SelectionChanged" SourceUpdated="LbData_SourceUpdated" >
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<CheckBox Margin="5,2"
IsChecked="{Binding IsSelected, UpdateSourceTrigger=PropertyChanged}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked">
<ContentPresenter />
</CheckBox>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
</ListBox>
<Grid>
<Frame x:Name="FrameDatas" Margin="829,46,0,0" BorderBrush="#FF3C3C3C" BorderThickness="1">
</Frame>
<Button x:Name="btnBack" Content=" ← " HorizontalAlignment="Left" Margin="10,5,0,0" VerticalAlignment="Top" Width="20"></Button>
<Button x:Name="btnForward" Content="→" HorizontalAlignment="Left" Margin="40,5,0,0" VerticalAlignment="Top" Width="20"/>
<Button x:Name="btnDirUp" Content="↑" HorizontalAlignment="Left" Margin="91,5,0,0" VerticalAlignment="Top" Width="20" Click="btnDirUp_Click"/>
<ProgressBar HorizontalAlignment="Left" Height="22" Margin="838,368,0,0" VerticalAlignment="Top" Width="422" x:Name="pgTesting"/>
</Grid>
<ListBox x:Name="lbSelectedItems" Margin="829,47,0,297" >
</ListBox>
<Button x:Name="btnTestReaction" Content="Start Backup" Width="136" Visibility="Visible" Click="btnTestReaction_Click" Margin="840,400,294,208"/>
<Grid Margin="828,27,0,0" Height="20" VerticalAlignment="Top">
</Grid>
<TextBox x:Name="tbCurrentPath" Height="23" Margin="169,4,0,0" TextWrapping="Wrap" VerticalAlignment="Top" HorizontalAlignment="Left" Width="1101" IsReadOnly="True" DataContextChanged="tbCurrentPath_DataContextChanged"/>
</Grid>
</Window>
What I have tried:
A lot but I'm still a beginner