silverlight domaindatasource binding to model
I have a user control that has a ViewModel behind it...
The constructor of the user control in codebehind looks like this:
public ClientListingVM()
{
ClientListingViewModel model = new ClientListingViewModel();//this is my model
DataContext = model;
InitializeComponent();
}
The control has inside it a DataGrid & Pager:
<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>
My model looks like this:
public class ClientListingViewModel : ViewModelBase
{
private DomainDataSource _clientDataSource;
public ClientListingViewModel()
{
ClientDataSource.Load();
}
public DomainDataSource 开发者_如何学GoClientDataSource
{
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; }
}
}
The problem is that the binding does not work and I am not able to figure out why..... If in the xaml I add the domaindatasource control and I make the binding to it then it works ok...
<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>
How can I make this work from the viewmodel?
I have fixed the problem by binding to ClientDataSource.Data
<controls:DataGrid x:Name="ClientGrid" MinHeight="100" IsReadOnly="True"
ItemsSource="{Binding Path=ClientDataSource.Data}"
AutoGenerateColumns="False" MinWidth="190" Height="200">
精彩评论