Addcommand
(カスタムコマンド)を使用して追加するときに新しいコーヒーの詳細を追加したいが、新しい詳細を追加するときはnull
なので、どうすればこの問題を回避できますか?UWPプロジェクトでMVVMを使用してnull値を回避するにはどうすればよいですか?
ModelクラスCoffee
public class Coffee : BindableBase
{
private int coffeeId;
private string coffeeName;
public int CoffeeId
{
get
{
return coffeeId;
}
set
{
coffeeId = value;
RaisePropertyChanged("CoffeeId");
}
}
public string CoffeeName
{
get
{
return coffeeName;
}
set
{
coffeeName = value;
RaisePropertyChanged("CoffeeName");
}
}
}
ビューCoffeeAdd.xaml
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Coffee Id"/>
<TextBox Width="120" Height="30" Margin="50 0 0 0" Text="{Binding CoffeeId}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 20 0 0">
<TextBlock Text="Coffee Name"/>
<TextBox Width="120" Height="30" Margin="20 0 0 0" Text="{Binding CoffeeName}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="0 20 0 0">
<Button Content="Add" Width="120" Height="30" Command="{Binding AddCommand}"/>
<Button Content="View" Width="120" Height="30" Margin="20 0 0 0"/>
</StackPanel>
</StackPanel>
のViewModel CoffeeAddViewModel
public class CoffeeAddViewModel:BindableBase
{
private ICoffeeDataService coffeedataservice;
public CoffeeAddViewModel(ICoffeeDataService dataservice)
{
coffeedataservice = dataservice;
LoadCommand();
}
private int _coffeeId;
private string _coffeeName;
public int CoffeeId
{
get
{
return _coffeeId;
}
set
{
_coffeeId = value;
RaisePropertyChanged("CoffeeId");
}
}
public string CoffeeName
{
get
{
return _coffeeName;
}
set
{
_coffeeName = value;
RaisePropertyChanged("CoffeeName");
}
}
public ICommand AddCommand { get; set; }
private void LoadCommand()
{
AddCommand = new CustomCommand(add, canadd);
}
private async void add(object obj)
{
coffeedataservice.AddCoffee(new Model.Coffee { CoffeeId = _coffeeId, CoffeeName = _coffeeName });
var dialog = new MessageDialog("Successfully Added");
await dialog.ShowAsync();
}
private bool canadd(object obj)
{
return true;
}
}
問題を回避するにはどうしたらいいですか? – Azarudeen