Initially I posted this to the PRISM4 forum but got a suggestion that I should try this forum as well:) I m using WPF4 BTW......
I m running PRISM4 and I ve been struggling to get my data binding to work. I m following the MVVM pattern and have a view model which initially loads data from a RDBMS and wraps it in an ICollectionView.This works perfectly, the data is displayed in the bound DatGrid, but I m struggling in my eforts when trying to persist changes made to the data which is presented in a DataGrid declared below.
The view model publishes the ICollectionView through a read/write property, "Results", which, as you can see has a binding mode of "TwoWay". I thought this would be enough to persist the changes made to the state of the checkboxes but no:( I ve experimented with a number of ways to accomplish this but the state of the checkbox is not propagated back to the view model. I ve intercepted the call to the "PlotClicked" method which is an ICommand object but the argument being passed has an unchanged "Plot" attribute! This is especially obvious when I click one of the column headers and the view is sorted - the checked rows are unchecked which is the default state of the checkboxes when retrieved from the db.
我在这里做了什么错误?
Many thanks in advance - I m really stuck here:( /Peter
<DataGrid Grid.Row="0" Name="gridResults" ItemsSource="{Binding Results,Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Plot">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Path=Plot, Mode=TwoWay}"
HorizontalAlignment="Center"
Command="{Binding Path=DataContext.PlotClicked,Mode=OneWay, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
......
我尝试了向我提出的建议。 这是我所做的事:
在图形一中,ICollectionView的复合财产改为OC。
公共观察 成果{获得;
添加了以下模板资源,用于形成观点的用户群
在“Columns”一节中添加以下代码:
<DataGridTemplateColumn Header="cbTest" x:Name="cbTest" CellTemplate="{StaticResource IsSelectedColumnTemplate}" CellEditingTemplate="{StaticResource IsSelectedColumnTemplateEditing}" CanUserSort="True" Width="Auto" />
After having made those changes I experimented with various settings for the UpdateSourceTrigger in the IsChecked="{Binding mentioned...... in (2) above with no effect. The changes I make to the checkboxes are not transferred back to the view-model s ObservableCollection.
同样,许多人感谢我努力帮助我来到这里!
* UPDATE * Now I ve experienced something REALLY SPOOOOKY:( This is what I ve done:
public class ResultViewResult : IReslutViewResult
{
public bool Plot { get; set; }
public Guid ResultId { get; set; }
public DateTime Generated { get; set; }
public int Duration { get; set; }
......
这样做是因为Plot 房产可以通过点击数据Grid中的检查箱子一栏而确定NEVER。 现在我做了以下工作:
public class ResultViewResult : IReslutViewResult
{
private bool _plot;
public bool Plot
{
get
{
return _plot;
}
set
{
_plot = value;
}
}
public Guid ResultId { get; set; }
public DateTime Generated { get; set; }
public Guid ResultId { get; set; }
public DateTime Generated { get; set; }
public int Duration { get; set; }
......
你可能要求的结果? 它是行之有效的,Plot是正确的。 现在,我想,这真是ir! 因此,我所做的是:
public class ResultViewResult : IReslutViewResult
{
public bool Plot { get; set; }
//private bool _plot = false;
//public bool Plot
//{
// get
// {
// return _plot;
// }
// set
// {
// _plot = value;
// }
//}
public Guid ResultId { get; set; }
public DateTime Generated { get; set; }
public int Duration { get; set; }
......
Ok, what about the result? IT WORKS!!!??? I m stunned...... I mean what s the difference between the first and the last???? I feel VERY awkward about this - I mean I want to know WHAT S going on behind the scene here...... :(