WPF向列添加任务

时间:2014-10-13 19:19:49

标签: c# wpf xaml

我是WPF的新手,我正在尝试在WPF中创建一个待办事项列表,但无法弄清楚如何在列中添加任务。 第一列将包含任务,其他列必须在其中自动生成“删除”按钮。 我不明白如何以编程方式添加任务。 这是XAML

<Window x:Class="ToDoList.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="416.932" Width="525" Background="#FF1B1A19">

    <Grid Margin="0,0,181,-49">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="155*"/>

            <ColumnDefinition Width="13*"/>

        </Grid.ColumnDefinitions>

        <Label Content="Task:" HorizontalAlignment="Left" Height="47" VerticalAlignment="Top" 

Width="57" FontSize="18" Foreground="#FFF7EFEF"/>

        <TextBox Name="userTask" Height="25" Margin="0,10,-24,0" TextWrapping="Wrap" 

VerticalAlignment="Top" Width="298" Grid.ColumnSpan="2" FontSize="16" HorizontalAlignment="Right" 

BorderThickness="2,1" KeyDown="userTask_KeyDown"/>

        <ListBox  Name="taskList"  Grid.ColumnSpan="5" Height="265" Margin="5,52,-157,0" 

VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch">

            <ListBox.ItemTemplate>

                <DataTemplate>

                    <Grid Margin="0,16">

                        <Grid.ColumnDefinitions>

                            <ColumnDefinition Width="*" Name="taskCol" />

                            <ColumnDefinition Width="25" Name="buttonCol" />

                        </Grid.ColumnDefinitions>

                        <TextBlock Name="testABC" />

                    </Grid>

                </DataTemplate>

            </ListBox.ItemTemplate>

           </ListBox>

       <Button Name="addTask" Content="Add" Grid.Column="1" HorizontalAlignment="Left" Height="25" 

Margin="91,10,-147,0" VerticalAlignment="Top" Width="82" Click="Button_Click"/>

    </Grid>


</Window> 

1 个答案:

答案 0 :(得分:1)

您应该使用WPF binding。因此,创建一个列表并将其分配给ListBox的ItemsSource。要在应用程序执行期间修改列表时获取UI更新,请使用ObservableCollection。在我的例子中,我只是将它绑定到字符串集合。

你的ListBox部分将如下所示(注意Text="{Binding}"部分 - 它绑定到一个字符串,如果你绑定到某个对象也使用Path那里和一个属性名称):

        <ListBox  Name="taskList"  Grid.ColumnSpan="5" Height="265" Margin="5,52,-157,0" 
VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True" HorizontalAlignment="Stretch">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Margin="0,16">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" Name="taskCol" />
                            <ColumnDefinition Width="25" Name="buttonCol" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Name="testABC" Text="{Binding}" Grid.Column="0" />
                        <Button Content="[x]" Grid.Column="1"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

它的代码隐藏:

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;


namespace ToDoList
{
    public partial class MainWindow
    {
        private ObservableCollection<string> _tasks;
        public MainWindow()
        {
            InitializeComponent();
            _tasks = new ObservableCollection<string>();
            taskList.ItemsSource = _tasks;
        }
        private void userTask_KeyDown(object sender, KeyEventArgs e)
        {

        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            _tasks.Add(userTask.Text);
        }
    }
}