UWP绑定编辑表单

时间:2017-03-23 12:34:04

标签: c# xaml uwp win-universal-app uwp-xaml

我使用SQLite-net来做CRUD,但我坚持将gridview中当前选中的元素绑定到不同帧中的控件。

CustomClass.cs

using ProjectName.SQLite;

namespace ProjectName.Models
{
    public class CustomClass
    {
        [PrimaryKey, AutoIncrement, Column("ID")]
        public int ID { get; set; }
        [Column("CustomText")]
        public string CustomText { get; set; }
    }

}

public static class SQLiteDataManager
{
    private static SQLiteAsyncConnection conn = new SQLiteAsyncConnection("localdatabase.db");

    public async static Task Create()
    {
        await conn.CreateTableAsync<CustomClass>();
    }

    public async static Task Drop()
    {
        await conn.DropTableAsync<CustomClass>();
    }

    public async static Task Add(CustomClass temp)
    {
        await conn.InsertAsync(temp);
    }

    public async static Task Delete(CustomClass temp)
    {
        await conn.Delete(temp);
    }

    public async static Task Select(ObservableCollection<CustomClass> temp)
    {
        temp.Clear();

        var results = await conn.Table<CustomClass>().ToListAsync();
        foreach (var result in results)
        {
            temp.Add(result);
        }
    }
}

app.xaml.cs - 存储实例以引用帧之间

using ProjetName.Models;

namespace ProjetName
{
    sealed partial class App : Application
    {
        public static CustomClass selectedGridViewItem { get; set; }
    }
}

ViewItemsFrame.xaml.cs - 选择网格视图后,将更新变量的应用程序级实例。编辑按钮显示在网格视图中选择项目后的编辑框架。

    private void butEdit_Click(object sender, RoutedEventArgs e)
    {
        Frame.Navigate(typeof(EditItemFrame));
    }

    private void gvData_ItemClick(object sender, ItemClickEventArgs e)
    {
        App.selectedGridViewItem = (CustomClass)e.ClickedItem;
    }

EditItemFrame.xaml - 编辑表单显示来自前一帧的gridview中所选项目的数据

<Page.DataContext>
    <vm:EditViewModel x:Name="ViewModel" />
</Page.DataContext>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Button Name="butSave" FontFamily="Segoe MDL2 Assets" Content="&#xE105;" FontSize="12" RelativePanel.LeftOf="butCancel" Click="butSave_Click" Margin="5,0,0,0" Width="46" Height="31" />
        <Button Name="butCancel" FontFamily="Segoe MDL2 Assets" Content="&#xE0D5;" FontSize="12" RelativePanel.AlignRightWithPanel="True" Click="butCancel_Click" Margin="5,0,0,0" Width="46" Height="31" />
        <TextBlock Text="Text stuff: "/>
        <TextBox Name="txtStuff" Text="{Binding ManageCustom.CustomText, Mode=TwoWay}" />
    </StackPanel>
</Grid>

EditItemFrame.xaml.cs

using ProjectName;

namespace ProjectName
{
    public sealed partial class EditItemsFrame : Page
    {
        CustomClass editedcustom;

        public EditCustom()
        {
            this.InitializeComponent();
            editedcustom = App.selectedGridViewItem;
        }

        private void butSave_Click(object sender, RoutedEventArgs e)
        {

        }

        private void butCancel_Click(object sender, RoutedEventArgs e)
        {
            Frame.Navigate(typeof(ViewItemsFrame));
        }
    }
}

EditViewModel.cs - 当编辑表单显示时,文本框将使用SelectedGridItem变量进行绑定/填充,但这种方式不起作用。不应该用双向自动保存吗?或者我应该一起废弃视图模型/绑定并在帧加载时分配文本框/列表框/日历/等值?

using ProjectName;
using ProjectName.Models;

namespace ProjectName.ViewModel
{
    class EditViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public CustomClass ManageCustom {
            get { return App.selectedGridViewItem; }
            set {
                App.selectedGridViewItem = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(App.selectedGridViewItem)));
            }
        }
    }
}

感谢任何帮助。

0 个答案:

没有答案