我有一个用户控件,后面有一个 ViewModel ......
代码隐藏中用户控件的构造函数如下所示:
public ClientListingVM()
{
ClientListingViewModel model = new ClientListingViewModel();//this is my model
DataContext = model;
InitializeComponent();
}
该控件内部有一个 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>
我的模型如下所示:
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 中添加域数据源控件并对其进行绑定,那么它可以正常工作......
<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>
如何从视图模型中完成这项工作?