0
我有了一個視圖模型背後的用戶控件...Silverlight的domaindatasource結合
在代碼隱藏的用戶控件的構造模型看起來是這樣的:
public ClientListingVM()
{
ClientListingViewModel model = new ClientListingViewModel();//this is my model
DataContext = model;
InitializeComponent();
}
控制其內部具有一個DataGrid &傳呼機:
<controls:DataGrid x:Name="ClientGrid" MinHeight="100" IsReadOnly="True"
ItemsSource="{Binding Path=Data}"
AutoGenerateColumns="False" MinWidth="190" Height="200">
<controls:DataGrid.Columns>
<!--<controls:DataGridTextColumn Binding="{Binding ClientId}" Header="Client ID"/>-->
<controls:DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"/>
<controls:DataGridTextColumn Binding="{Binding LastName}" Header="LastName"/>
<controls:DataGridTextColumn Binding="{Binding Phone}" Header="Phone"/>
<controls:DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
<controls:DataGridTextColumn Binding="{Binding Address}" Header="Address"/>
<controls:DataGridTextColumn Binding="{Binding CNP}" Header="CNP"/>
<controls:DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<controls:DataGridTextColumn Binding="{Binding CUI}" Header="CUI"/>
<controls:DataGridTextColumn Binding="{Binding CIF}" Header="CIF"/>
<controls:DataGridTextColumn Binding="{Binding BankAccount}" Header="BankAccount"/>
<controls:DataGridTemplateColumn Header="Photo">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="ClientImage" Source="{Binding Picture, Converter={StaticResource ImageConverter}}" Stretch="UniformToFill" Width="150" Height="150" />
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Header="Action">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="DeleteClient" Click="DeleteClient_Click" CommandParameter="{Binding ClientId}" Content="Delete"></Button>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
<controls:DataPager x:Name="ContactPager" PageSize="5" Source="{Binding Path=Data}" MinWidth="190"></controls:DataPager>
我的模型看起來是這樣的:
public class ClientListingViewModel : ViewModelBase
{
private DomainDataSource _clientDataSource;
public ClientListingViewModel()
{
ClientDataSource.Load();
}
public DomainDataSource ClientDataSource
{
get {
if (_clientDataSource == null) {
_clientDataSource = new DomainDataSource();
_clientDataSource.DomainContext = new ClientDomainContext();
//_clientDataSource.AutoLoad = true;
_clientDataSource.LoadSize = 10;
_clientDataSource.QueryName = "GetClientsQuery";
_clientDataSource.LoadedData += new EventHandler<LoadedDataEventArgs>(_clientDataSource_LoadedData);
}
return _clientDataSource;
}
}
void _clientDataSource_LoadedData(object sender, LoadedDataEventArgs e){
OnPropertyChanged("Data");
}
public DomainDataSourceView Data
{
get { return ClientDataSource.DataView; }
}
}
問題是綁定不起作用,我無法弄清楚爲什麼..... 如果在xaml中添加了domaindatasource控件,並對其進行綁定,那麼它就可以正常工作。 ..
<riacontrols:DomainDataSource x:Name="ClientDataSource" AutoLoad="True" LoadSize="10" QueryName="GetClients" >
<riacontrols:DomainDataSource.DomainContext>
<ds:ClientDomainContext/>
</riacontrols:DomainDataSource.DomainContext>
</riacontrols:DomainDataSource>
<controls:DataGrid x:Name="ClientGrid" MinHeight="100" IsReadOnly="True"
ItemsSource="{Binding ElementName=ClientDataSource, Path=DataView}"
AutoGenerateColumns="False" MinWidth="190" Height="350">
<controls:DataGrid.Columns>
<!--<controls:DataGridTextColumn Binding="{Binding ClientId}" Header="Client ID"/>-->
<controls:DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"/>
<controls:DataGridTextColumn Binding="{Binding LastName}" Header="LastName"/>
<controls:DataGridTextColumn Binding="{Binding Phone}" Header="Phone"/>
<controls:DataGridTextColumn Binding="{Binding Email}" Header="Email"/>
<controls:DataGridTextColumn Binding="{Binding Address}" Header="Address"/>
<controls:DataGridTextColumn Binding="{Binding CNP}" Header="CNP"/>
<controls:DataGridTextColumn Binding="{Binding Name}" Header="Name"/>
<controls:DataGridTextColumn Binding="{Binding CUI}" Header="CUI"/>
<controls:DataGridTextColumn Binding="{Binding CIF}" Header="CIF"/>
<controls:DataGridTextColumn Binding="{Binding BankAccount}" Header="BankAccount"/>
<controls:DataGridTemplateColumn Header="Photo">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="ClientImage" Source="{Binding Picture, Converter={StaticResource ImageConverter}}" Stretch="UniformToFill" Width="150" Height="150" />
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
<controls:DataGridTemplateColumn Header="Action">
<controls:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button x:Name="DeleteClient" Click="DeleteClient_Click" CommandParameter="{Binding ClientId}" Content="Delete"></Button>
</DataTemplate>
</controls:DataGridTemplateColumn.CellTemplate>
</controls:DataGridTemplateColumn>
</controls:DataGrid.Columns>
</controls:DataGrid>
<controls:DataPager x:Name="ContactPager" PageSize="5" Source="{Binding ElementName=ClientDataSource, Path=Data}" MinWidth="190"></controls:DataPager>
如何使從視圖模型這項工作?