I am trying to do a little To-Do app for my school project. I have one problem: i don't know how to delete items both generated by data template in app and those in database.
I've tried accesing items by getting selected item and then deleting it but at some point the id's of those items in db are diffrent from those in the app. I am using SQL server and in my db i have one table with 4 columns: ID(int, auto incremented, primary key), Task(varchar), Descr(varchar), Active(bit). Now i am trying to bind checkbox attribute isChecked to Active of Task class in my app.
this is my xaml code
<Window x:Class="ToDoApp2.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:ToDoApp2"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="400" ResizeMode="NoResize">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="244*"/>
<ColumnDefinition Width="149*"/>
</Grid.ColumnDefinitions>
<TreeView x:Name="TrvMenu" HorizontalAlignment="Left" Height="400" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:ToDoTask}" ItemsSource="{Binding Tasks}">
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Title}" IsChecked="{Binding active}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
<TextBox x:Name="TaskTb" HorizontalAlignment="Left" Height="30" Margin="0,400,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2"/>
<TextBox x:Name="DescriptionTb" HorizontalAlignment="Left" Height="80" Margin="0,430,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="2"/>
<Button x:Name="CreateBtn" Content="Create New Task" HorizontalAlignment="Left" Margin="0,510,0,0" VerticalAlignment="Top" Width="197" Height="59" Click="Button_Click"/>
<Button x:Name="DeleteBtn" Content="Delete Selected Task" HorizontalAlignment="Left" Margin="197,510,-1,0" VerticalAlignment="Top" Width="196" Height="59" Click="DeleteBtn_Click" Grid.ColumnSpan="2"/>
</Grid>
</Window>
this is the class that represents one task in app
public class ToDoTask
{
public ToDoTask()
{
this.Tasks = new ObservableCollection<ToDoTask>();
}
public string Title { get; set; }
public bool active=true;
public ObservableCollection<ToDoTask> Tasks { get; set; }
}
And this is how i add new tasks to db and app
public MainWindow()
{
InitializeComponent();
SQLCnn init = new SQLCnn();
ObservableCollection<ToDoTask> initList = init.readQuery();
for(int i=0; i < initList.Count; i++)
{
TrvMenu.Items.Add(initList[i]);
}
SQLCnn.connection.Close();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(TaskTb.Text))
{
string value = TaskTb.Text;
string desc = DescriptionTb.Text;
ToDoTask task = new ToDoTask() { Title = value };
task.Tasks.Add(new ToDoTask() { Title = desc });
SQLCnn SQLtask = new SQLCnn();
SQLtask.insertQuery(value, desc);
TrvMenu.Items.Add(task);
}
}
}