我正在尝试了解C#mvvm和wpf,所以我决定制作一个简单的项目(一本书和读者管理图书馆的应用程序),现在我有点迷失,所以我真的很感激一些帮助。 在我看来,我有一个列表视图显示读者,以及一组文本框绑定到列表视图的选定项目(名称,地址等)。 listview绑定到使用Linq2SQL从数据库表(读者)创建的可观察的读者集合。
在文本框下方,我有几个按钮(清除/添加/保存/删除)。
我遇到的问题是"所选项目"如果我在单击按钮时尝试执行某些命令,则列表视图似乎为null。
当我点击清除按钮时,如果我在所选项目(阅读器)上有一个断点,它会显示它为空/空间隔,但文本框不清楚; 如果在Clear方法中使用了SelectedReader属性,并为其属性分配了空格,则列表视图中的所选项也会清除(因为它绑定到Reader的FullName属性)
我在添加新阅读器和保存更改方面存在其他问题,我认为是因为错误的linq查询,但我希望我能解决当前的问题并继续前进。
我该怎么办?
视图模型
public class MainWindowViewModel : ViewModelBase
{
private Reader selectedReader;
private ObservableCollection<Reader> readerList;
public MainWindowViewModel()
{
SelectedReader = new Reader()
{
FullName = "",
SerialNumber = "",
IdNumber = "",
Adress = "",
AltContactMethods = ""
};
BookDBDataContext rdb = new BookDBDataContext();
ReadersList = new ObservableCollection<Reader>(rdb.Readers);
AddR = new TblQryCommand(AddToDb);
EditR = new TblQryCommand(EditToDb);
DeleteR = new TblQryCommand(DeleteFromDb);
ClearR = new TblQryCommand(ClearReaderFields);
}
public TblQryCommand AddR { get; private set; }
public TblQryCommand EditR { get; private set; }
public TblQryCommand DeleteR { get; private set; }
public TblQryCommand ClearR { get; private set; }
//Reader List
public ObservableCollection<Reader> ReadersList
{
get { return readerList; }
set
{
if (readerList != value)
{
readerList = value;
RaisePropertyChanged();
}
}
}
public Reader SelectedReader
{
get { return selectedReader; }
set
{
if (selectedReader != value)
{
selectedReader = value;
RaisePropertyChanged();
}
}
}
public void AddToDb()
{
BookDBDataContext db = new BookDBDataContext();
Reader r = new Reader
{
FullName = SelectedReader.FullName,
SerialNumber = SelectedReader.SerialNumber,
IdNumber = SelectedReader.IdNumber,
Adress = SelectedReader.Adress,
AltContactMethods = SelectedReader.AltContactMethods
};
db.Readers.InsertOnSubmit(r);
db.Readers.Context.SubmitChanges();
}
public void DeleteFromDb()
{
}
public void EditToDb()
{
}
public void ClearReaderFields()
{
SelectedReader = new Reader
{
Id = 0,
FullName = string.Empty,
SerialNumber = string.Empty,
IdNumber = string.Empty,
Adress = string.Empty,
AltContactMethods = string.Empty
};
}
查看:
<ListView Name="listviewReaders" ItemsSource="{Binding ReadersList}" SelectedItem="{Binding SelectedReader,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="140" Height="180" Margin="10,68,492,281">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding FullName}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<TextBox x:Name="txtBxFullName" HorizontalAlignment="Left" Height="22" Margin="211,68,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
<TextBox x:Name="txtBxSerialNumber" HorizontalAlignment="Left" Height="22" Margin="211,95,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.SerialNumber, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
<TextBox x:Name="txtBxIdNumber" HorizontalAlignment="Left" Height="22" Margin="211,122,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.IdNumber, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
<TextBox x:Name="txtBxAdress" HorizontalAlignment="Left" Height="22" Margin="211,149,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.Adress, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
<TextBox x:Name="txtBxAltContactMethods" HorizontalAlignment="Left" Height="22" Margin="211,176,0,0" TextWrapping="Wrap" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.AltContactMethods, Mode=TwoWay, UpdateSourceTrigger=Explicit}" VerticalAlignment="Top" Width="315"/>
<Button x:Name="btnReader_Clear" Content="Clear" Command="{Binding ClearR}" HorizontalAlignment="Left" Margin="211,228,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnReader_Save" Content="Save" Command="{Binding EditR}" HorizontalAlignment="Left" Margin="291,228,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnReader_Add" Content="Add New" Command="{Binding AddR}" HorizontalAlignment="Left" Margin="371,228,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnReader_Delete" Content="Delete" Command="{Binding DeleteR}" HorizontalAlignment="Left" Margin="451,228,0,0" VerticalAlignment="Top" Width="75"/>
答案 0 :(得分:0)
您是否曾尝试将文本框的Text-Property的Binding
直接设置为ViewModel&#39; s SelectedReader
- 属性?
所以不要写这个:
<TextBox x:Name="txtBxFullName" Text="{Binding ElementName=listviewReaders, Path=SelectedItem.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" ... />
你会写:
<TextBox x:Name="txtBxFullName" Text="{Binding Path=SelectedReader.FullName, Mode=TwoWay, UpdateSourceTrigger=Explicit}" ... />