簡體   English   中英

從用戶控件WPF MVVM保存數據

[英]Saving Data from a user control WPF MVVM

我有兩個用戶控件,每個用戶控件都包含一個numericInput,這些控件使用ItemsControl ItemTemplate添加到我的MainWindow中的列表框。 它們都像這樣綁定到Ilist屬性,在我的ClientRatesViewModel(用於MainWindow的屬性)中,我需要將每個值保存在SaveData方法的“數值輸入”中,如何實現呢?

MainWindow.xaml(已刪除某些代碼)

    <ListBox x:Name="lbPostAwr" ItemsSource="{Binding ClientRatesPostAwrDescription}"
        KeyboardNavigation.TabNavigation="Continue" Margin="548,23,10,69" av:Grid.Row="1">
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0" Focusable="False">
                    <UserControls:PostAWR />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListBox>

ClientRatesViewModel(已刪除某些代碼)

 public IList<ClientRates> ClientRatesPreAwrValue
    {
        get { return _clientRatePreAwrValue; }
        set
        {
            _clientRatePreAwrValue = value;
            OnPropertyChanged("ClientRatesPreAwrValue");
        }
    }

    public IList<ClientRates> ClientRatesPostAwrValue
    {
        get { return _clientRatePostAwrValue; }
        set
        {
            _clientRatePostAwrValue = value;
            OnPropertyChanged("ClientRatesPostAwrValue");
        }
    }

 private bool SaveData() ///QQQ
    {
        bool isSaved = false;

        try
        {
            using (var connection = new SqlConnection(ExtensionMethods.ConnectionString))
            {
                connection.Open();
                using (var command = new SqlCommand("SPHERE", connection))
                {
                    command.CommandType = CommandType.StoredProcedure;

                    //foreach (var test in this.)
                    //{
                    //    command.Parameters.Add(rate.ClientRatesPreAwr,
                    //}

                    int rowsAffected = command.ExecuteNonQuery();
                    {


                    }
                }
            }
            isSaved = true;
        }

        catch (Exception ex)
        {
            throw;
        }
        return isSaved;


public IList<ClientRates> ClientRatesPreAwrValue
    {
        get { return _clientRatePreAwrValue; }
        set
        {
            _clientRatePreAwrValue = value;
            OnPropertyChanged("ClientRatesPreAwrValue");
        }
    }

    public IList<ClientRates> ClientRatesPostAwrValue
    {
        get { return _clientRatePostAwrValue; }
        set
        {
            _clientRatePostAwrValue = value;
            OnPropertyChanged("ClientRatesPostAwrValue");
        }
    }

我的ClientsRates模型如下所示:

    private string _clientRatesPreAwrDescription;
    private double _clientRatePreAwrValue;
    private double _clientRatePostAwrValue;
    private string _clientRatePostAwrDescription;

    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public string ClientRatesPreAwrDescription
    {
        get { return _clientRatesPreAwrDescription; }
        set
        {
            _clientRatesPreAwrDescription = value;
            OnPropertyChanged("ClientRatesPreAWR");
        }
    }

    public double ClientRatesPreAwrValue
    {
        get { return _clientRatePreAwrValue; }
        set
        {
            _clientRatePreAwrValue = value;
            OnPropertyChanged("ClientRatesPreAwrValue");
        }
    }

    public double ClientRatesPostAwrValue
    {
        get { return _clientRatePostAwrValue; }
        set
        {
            _clientRatePostAwrValue = value;
            OnPropertyChanged("ClientRatesPostAwrValue");
        }
    }

    public string ClientRatesPostAwrDescription
    {
        get { return _clientRatePostAwrDescription; }
        set
        {
            _clientRatePostAwrDescription = value;
            OnPropertyChanged("ClientRatesPostAwr");
        }
    }
}

ListBox的ItemsSource必須綁定到一個集合,而不是字符串屬性( ClientRatesPostAwrDescription )。 從以下視圖開始:

<Window.DataContext>
    <vm:ViewModel /> <!--Or set the DataContext in another way-->
</Window.DataContext>
<StackPanel>
    <ListBox x:Name="lbPostAwr" ItemsSource="{Binding ClientRatesPostAwr}">
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0" Focusable="False">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Column="0" Content="{Binding Description}" />
                    <TextBox Grid.Column="1" Text="{Binding Value}" />
                </Grid>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ListBox>
    <Button Content="Save" Command="{Binding SaveCommand, Mode=OneWay}" />
</StackPanel>

並查看模型:

public class ClientRateViewModel
{
    public string Description { get; set; }
    public int Value { get; set; }
}

public class RelayCommand : ICommand
{
    Action<object> _execute;

    public RelayCommand(Action<object> execute) {
        _execute = execute;
    }

    public void Execute(object parameter) {
        _execute(parameter);
    }

    public bool CanExecute(object parameter) {
        return true;
    }

    public event EventHandler CanExecuteChanged;
}

public class ViewModel
{
    public ViewModel() {
        ClientRatesPostAwr = new List<ClientRateViewModel>();
        ClientRatesPostAwr.Add(new ClientRateViewModel { Description = "description 1", Value = 1 });
        ClientRatesPostAwr.Add(new ClientRateViewModel { Description = "description 2", Value = 2 });
        SaveCommand = new RelayCommand((o) => {
            MessageBox.Show("Saving: " + string.Join(",", ClientRatesPostAwr.Select(cr => cr.Value.ToString())));
        });
    }

    public IList<ClientRateViewModel> ClientRatesPostAwr { get; private set; }

    public ICommand SaveCommand { get; private set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM