English 中文(简体)
数据问题——数据记录/记录
原标题:DataBinding issue - DataTemplate/ViewModel/ICollectionView
  • 时间:2011-07-25 08:04:21
  •  标签:
  • wpf

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>

......

我尝试了向我提出的建议。 这是我所做的事:

  1. 在图形一中,ICollectionView的复合财产改为OC。

    公共观察 成果{获得;

  2. 添加了以下模板资源,用于形成观点的用户群

  3. 在“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...... :(

问题回答

不知道这一点,但我建议你尝试一个<条码>。 ObservableCollection used as 项目Source。 我有许多问题,如以前一样,所有这些问题都是通过这种收集而得到解决的(这比为复健目的进行的传统收集要快,而且少费)。

此外,还试图在<代码>中加入。 IsChecked binding the following:UpdateSourceTrigger=PropertyChanged, This could do the trick, 我想,这里的唯一问题是,来文方在适当的时候已经更新!

Not sure if this is the issue in your case, but the DataGrid uses a variation of databinding that will not commit the changes to the source until you move off the row. This is called a BindingGroup. Maybe you re not seeing the values committed because you haven t moved off the row yet?

http://blogs.msdn.com/b/vinsibal/archive/2008/08/11/wpf-3-5-sp1-feature-bindinggroups-with-item-level-validation.aspx

另一种可能性是,具有约束力的道路有些不正确? 你们是否检查了VS的产出窗口,看看看它是否报告任何具有约束力的道路故障?





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...