我是MVVM的新手。我有填充Employee列表的列表框。我使用MVVM光Silverlight4列表框對象和按鈕
<ListBox BorderBrush="Transparent"
HorizontalContentAlignment="Stretch"
VerticalAlignment="Stretch"
Name="EmployeeListBox"
Background="Transparent"
ItemsSource="{Binding Employees}"
SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="4">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="200"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="300"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<sdk:Label Grid.Row="0" Grid.Column="0"
Content="Name" />
<TextBox Grid.Column="1" Grid.Row="0"
Name="EmployeeName"
Height="23"
Text="{Binding Path=Name, Mode=TwoWay}"
VerticalAlignment="Center"
HorizontalAlignment="Stretch" />
<Button Grid.Column="2" Grid.Row="0"
Name="RefresEmployeeName"
Width="20"
Height="25"
Command="{Binding RefreshEmployeeNameCommand}"
Content="Refresh" />
<Button Grid.Column="4" Grid.Row="0"
Name="DeleteEmployee"
Width="20"
Height="25"
Content="Delete"
Command="{Binding DeleteEmployeeCommand}" />
<sdk:Label Grid.Row="1" Grid.Column="0"
Content="Description" />
<TextBox Grid.Column="1" Grid.Row="1" Height="23"
Text="Binding Path=Description}"
VerticalAlignment="Center"
HorizontalAlignment="Stretch" />
<Button Grid.Column="2" Grid.Row="1"
Name="RefreshDescription"
Width="20"
Height="25"
Content="Refresh"
Command="RefreshDescriptionCommand" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
這將顯示在員工面前diaplyed與名稱,並在文本框中顯示的描述和接下來的一個刷新按鈕,員工姓名和說明文本框和一個刪除按鈕的員工列表名稱文本框。
在我的ViewModel我已經創建了3 RelayCommands
作爲
RelayCommand RefreshEmployeeNameCommand = new RelayCommand(RefreshEmployeeName);
RelayCommand DeleteEmployeeCommand = new RelayCommand(DeleteEmployee);
RelayCommand RefreshDescriptionCommand = new RelayCommand(RefreshDescription);
當我點擊RefreshEmployeeName
按鈕,在文本框中的內容應該得到重置爲初始值,如果任何的編輯的員工的名字製成。 當我點擊DeleteEmployee
按鈕時,應該刪除SelectedEmployee
。 當我點擊RefreshDescription
按鈕時,應該重置對描述的任何編輯。
當我點擊RefreshEmployeeName
時,RefreshEmployeeName
函數沒有在ViewModel中調用。如何捕獲與此按鈕關聯的文本框中的數據。還有如何在單擊按鈕時捕獲選定的項目。當我點擊邊框附近時,選定的項目會更新。
這裏是我的視圖模型:
public IServiceAgent ServiceAgent { get; set; }
public EditViewModel(IServiceAgent serviceAgent)
{
if (!IsDesignTime)
{
if (serviceAgent != null)
{
ServiceAgent = serviceAgent;
}
GetEmployees();
WireCommands();
}
}
private void WireCommands()
{
RefreshEmployeeNameCommand = new RelayCommand(RefreshEmployeeName);
DeleteEmployeeCommand = new RelayCommand(DeleteEmployee);
RefreshEmployeeDescriptionCommand = new RelayCommand(RefreshEmployeeDescription);
}
public RelayCommand RefreshEmployeeNameCommand { get; private set; }
public RelayCommand DeleteEmployeeCommand { get; private set; }
public RelayCommand RefreshEmployeeDescriptionCommand { get; private set; }
private void RefreshEmployeeName()
{
// have to capture the value within the textbox employee name
}
private void DeleteEmployee()
{
// have to capture the employee object which should be deleted
}
private void RefreshEmployeeDescription()
{
// have to capture the value within the textbox employee description
}
private ObservableCollection<Employee> _Employees;
public ObservableCollection<Employee> Employees
{
get
{
return _Employees
}
set
{
if _Employees!= value)
{
_Employees= value;
OnPropertyChanged("Employees");
}
}
}
public Employee _SelectedEmployee;
public DisplayDevice SelectedEmployee
{
get
{
return _SelectedEmployee;
}
set
{
if (_SelectedEmployee!= value)
{
_SelectedEmployee= value;
OnPropertyChanged("SelectedEmployee");
}
}
}
private void GetEmployees()
{
ServiceAgent.GetEmployees((s, e) => Employees = e.Result);
}
請發表您的ViewModel。 – cadrell0 2012-02-27 21:38:22
我發佈了我的虛擬機。請讓我知道我錯在哪裏 – 2012-02-27 22:30:44