0

我有一个带有 tabcontrol 的页面。

我在控件中绑定了一个 tabitem 的位置列表。

记录在列表视图中列出。

我可以通过将输入控件绑定到 listview.selecteditem 来编辑记录。

我的问题是当我想添加新记录时。我想最小化后面的代码。

视图模型:

private ObservableCollection<LocationViewModel> _locations;

    public ObservableCollection<LocationViewModel> Locations
    {
        get { return _locations; }
    }

    public LocationListViewModel()
    {
        _locations = new ObservableCollection<LocationViewModel>();

        foreach (Service.Location l in service.GetLocationList().OrderBy(l => l.Building).ThenBy(l => l.Floor))
        {
            _locations.Add(new LocationViewModel
                                {
                                    id = l.id,
                                    Building = l.Building,
                                    Floor = l.Floor,
                                    RoomNo = l.RoomNo,
                                    MapTitle = l.MapTitle,
                                    MapExtension = l.MapExtension,
                                    Map = l.Map,
                                    DateCreated = l.DateCreated,
                                    CreatedByID = l.CreatedByID,
                                    CreatedByDesc = l.CreatedByDesc,
                                    DateEdited = l.DateEdited,
                                    EditedByID = l.EditedByID,
                                    EditedByDesc = l.EditedByDesc
                                }
                            );
        }
    }

XML:

<TabItem x:Name="tabSettingsLocations" x:Uid="tabSettingsLocations" 
    Header="Locations"
    DataContext="{StaticResource ResourceKey=LocationList}"> .....

成功绑定到列表视图以进行编辑的示例

<TextBox x:Name="txtSettingLocationBuildingEdit" 
    Margin="90,17,0,0" Style="{DynamicResource SettingsTextBoxStyle}"
    Text="{Binding SelectedItem.Building, ElementName=lvwSettingsLocations,
    Mode=TwoWay}" />

新记录绑定失败示例(使用不同的输入控件集)

<TextBox x:Name="txtSettingLocationBuildingAdd"  
    Margin="90,17,0,0" Style="{DynamicResource SettingsTextBoxStyle}" 
    Text="{Binding Building, ElementName=lvwSettingsLocations, 
    Mode=OneWayToSource}"/>

我还尝试将子选项卡项绑定到同一个数据源

<TabItem x:Name="tbSettingsLocationsAdd" x:Uid="tbSettingsLocationsAdd" 
    Header="Add New"
    DataContext="{StaticResource ResourceKey=LocationList}">

<TextBox x:Name="txtSettingLocationBuildingAdd"  
    Margin="90,17,0,0" Style="{DynamicResource SettingsTextBoxStyle}" 
    Text="{Binding Building}"/>

无济于事。

我还尝试创建一个新的子数据视图,但我希望将它们全部绑定在一起,以便界面更新我添加或编辑的任何内容。

有人帮忙吗?

4

1 回答 1

0

好的,所以我最终确定了这一点。只是想分享...感谢 Silvermind 提供有关最佳实践的良好提示。

命令:

class Location_Add : ICommand
{
    private ObservableCollection<LocationViewModel> _llvm;

    public ObservableCollection<LocationViewModel> llvm
    {
        get { return _llvm; }
    }

    public Location_Add(ObservableCollection<LocationViewModel> passedllvm)
    {
        _llvm = passedllvm;
    }

    public bool CanExecute(object parameter)
    {
        LocationViewModel lvw = parameter as LocationViewModel;
        return lvw != null;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
            LocationViewModel lvm = parameter as LocationViewModel;
            llvm.Add(lvm);
            AddLocation(lvm);
    }

    public void RaiseCanExecuteChanged()
    {
        var handler = CanExecuteChanged;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }

    public void AddLocation(LocationViewModel lvm)
    {
        try
        {
            Service.SchoolMonitorServiceClient service = new Service.SchoolMonitorServiceClient();

            Service.Location loc = new Service.Location();
            loc.Building = lvm.Building.Trim();
            loc.Floor = lvm.Floor.Trim();
            loc.RoomNo = lvm.RoomNo.Trim();
            loc.MapTitle = lvm.MapTitle;
            loc.MapExtension = lvm.MapTitle.Substring(lvm.MapTitle.IndexOf("."));
            loc.Map = lvm.Map;
            loc.DateCreated = DateTime.Now;
            loc.CreatedByID = (Int32)Application.Current.Resources["UserID"];
            loc.DateEdited = lvm.DateEdited;

            service.AddLocation(loc);
            MessageBox.Show("Your new Location was entered successfully", "Success", MessageBoxButton.OK);
        }
        catch (Exception e)
        {
            .....
        }
    }
}

视图模型:

class LocationListViewModel
{
    Service.SchoolMonitorServiceClient service = new Service.SchoolMonitorServiceClient();

    #region Members

    private ObservableCollection<LocationViewModel> _locations;
    private Location_Add _AddCommand;

    #endregion

    #region Properties

    public ObservableCollection<LocationViewModel> Locations
    {
        get { return _locations; }
    }

    #endregion

    public LocationListViewModel()
    {
        _locations = new ObservableCollection<LocationViewModel>();

        foreach (Service.Location l 
            in service.GetLocationList()
            .OrderBy(l => l.Building).ThenBy(l => l.Floor))
        {
            _locations.Add(new LocationViewModel
                                {
                                    id = l.id,
                                    Building = l.Building,
                                    Floor = l.Floor,
                                    RoomNo = l.RoomNo,
                                    MapTitle = l.MapTitle,
                                    MapExtension = l.MapExtension,
                                    Map = l.Map,
                                    DateCreated = l.DateCreated,
                                    CreatedByID = l.CreatedByID,
                                    CreatedByDesc = l.CreatedByDesc,
                                    DateEdited = l.DateEdited,
                                    EditedByID = l.EditedByID,
                                    EditedByDesc = l.EditedByDesc
                                }
                            );
        }

        _AddCommand = new Location_Add(_locations);
    }

    public ICommand AddCommand
    {
        get
        {
            return _AddCommand;
        }
    }
}

XML:

xmlns:local="clr-namespace:SchoolMonitor_WPF.ViewModels"

<Page.Resources>
    <local:LocationListViewModel x:Key="LocationList" />
    <local:LocationViewModel x:Key="NewLocation" />
</Page.Resources>

<TextBox x:Name="txtSettingLocationBuildingAdd" x:Uid="txtSettingLocationBuildingAdd"  
    Margin="90,17,0,0" Style="{DynamicResource SettingsTextBoxStyle}"
    DataContext="{StaticResource ResourceKey=NewLocation}"
    Text="{Binding Path=Building}"/>

<Button x:Name="btnSettingsLocationSaveAdd" Content="Submit" Margin="0,80,10,0" 
    VerticalAlignment="Top" Style="{DynamicResource ButtonStyle}"
    HorizontalAlignment="Right" Width="75"
    DataContext="{StaticResource ResourceKey=LocationList}"
    CommandParameter="{StaticResource ResourceKey=NewLocation}" 
    Command="{Binding AddCommand}">

希望这可以帮助某人。

于 2013-03-11T10:14:06.277 回答