0
任何人都可以幫助以下,我已經圍繞了一段時間,我不能使它的工作。 我想從列表框wpf mvvm保存數據並將其添加到列表並綁定列表框。wpf綁定列表框項目到一個對象
我有一個視圖模型:
private const string StagePropertyName = "Stage";
public string Stage
{
get
{
return _newProduct.Stage;
}
set
{
_newProduct.Stage = value;
RaisePropertyChanged(StagePropertyName);
}
}
public MainViewModel()
{
_newProduct = new Product();
CreateAddCommand();
}
private void CreateAddCommand()
{
AddCommand = new RelayCommand(AddExecute, CanExecuteAddCommand);
}
public void AddExecute()
{
Product.Add(_newProduct);
}
和XAML:我有約束力lstStage的的SelectedItem/Value屬性
<ListBox Grid.Column="1" Grid.Row="2" Grid.RowSpan="2" Height="23" HorizontalAlignment="Left" Margin="20,5,0,0" Name="lstStage" VerticalAlignment="Top" Width="120" SelectedValuePath="Value" SelectedValue="{Binding Path=Stage, Mode=TwoWay}">
<ListBoxItem>Item1</ListBoxItem>
<ListBoxItem>Item2</ListBoxItem>
<ListBoxItem>Item3</ListBoxItem>
</ListBox>
<Button Content="Add" Grid.Column="1" Grid.Row="6" Height="23" HorizontalAlignment="Left" Margin="25,10,0,0" Name="button1" VerticalAlignment="Top" Width="75" Command="{Binding Path=AddCommand}" />
public class Product
{
public string Name { get; set; }
public string Deposit { get; set; }
public string Lot { get; set; }
public string Stage { get; set; }
public string City { get; set; }
public static void Add(Product product)
{
MessageBox.Show(product.Stage); //here is null
}
}
麻煩。
請指教。
請顯示屬性/類'Product',其中暴露了'Add'方法來添加Product類的實例。也請顯示屬性'AddCommand' – pushpraj 2014-09-11 13:17:09
更新代碼請看看 – user2483797 2014-09-11 14:00:52
嘗試刪除'SelectedValuePath =「Value」SelectedValue =「{Binding Path = Stage,Mode = TwoWay}」'並添加'SelectedItem =「{Binding Path = Stage,Mode = TwoWay}「'看看你是否仍然面臨這個問題。 – pushpraj 2014-09-11 14:05:30