0

タブコントロールのあるページがあります。

コントロール内のタブ項目の場所のリストをバインドしています。

レコードはリストビューに表示されます。

入力コントロールを 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 に答える