1

我有一个绑定到下面 ViewModel 的 ObservableCollection 的数据网格。Datagrid 将正确显示所有值,因此绑定似乎正在工作,但如果我更改某些值,Grid 将不会调用我的 VM 的设置器。有人能告诉我为什么吗?

这是我的视图模型:

    public class DocumentVm : ViewModelBase
{
    private Document document;
    public bool IsNew { get; private set; }
    public Document Document {
        get { return document; }
    }

    public DocumentVm(Document document)
    {
        this.document = document;
        IsNew = false;
    }

    public DocumentVm(Document document, bool isNew)
    {
        this.document = document;
        IsNew = isNew;
    }

    public String Name 
    {
        get { return document.Name; }
        set { document.Name = value; RaisePropertyChangedEvent("Name");}
    }

    public String Path
    {
        get { return document.Path; }
        set { document.Path = value; }
    }

    public String Metadata
    {
        get { return document.Metadata; }
        set { document.Metadata = value; }
    }

    public int SpeechId
    {
        get { return document.SpeechId; }
        set { document.SpeechId = value; }
    }

}

这是 XAML 代码:

<DataGrid Margin="3" Grid.Row="7" Grid.Column="1" BorderThickness="0" 
      ItemsSource="{Binding Path=CurrentSpeech.Documents, Mode=TwoWay}" 
      SelectedItem="{Binding Path=CurrentSpeech.CurrentDocument, Mode=TwoWay}">
<DataGrid.Columns>
    <DataGridTemplateColumn Header="Name" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Name, Mode=TwoWay}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="MetaDaten" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Metadata, Mode=TwoWay}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
    <DataGridTemplateColumn Header="Pfad" Width="SizeToCells">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path}" />
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

谢谢你们!

4

2 回答 2

1

UpdateSourceTrigger=缺少绑定上的PropertyChanged。

于 2012-12-26T13:24:58.993 回答
0

Not sure why the setters aren't getting called but if you use dependency properties they actually do get called. I'm far too drunk to investigate why the CLR properties aren't getting set in your project but this works for me.

public partial class MainWindow : Window
{        public ObservableCollection<DocumentVm> Items
    {
        get { return (ObservableCollection<DocumentVm>)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }
    public static readonly DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items", typeof(ObservableCollection<DocumentVm>), typeof(MainWindow), new PropertyMetadata(null));

    public MainWindow()
    {
        InitializeComponent();
        Items = new ObservableCollection<DocumentVm>();
        Items.Add(new DocumentVm() { Name = "Name 1"});
        Items.Add(new DocumentVm() { Name = "Name 2"});
    }
}

public class DocumentVm : DependencyObject
{
    public string Name
    {
        get { return (string)GetValue(NameProperty); }
        set { SetValue(NameProperty, value); }
    }
    public static readonly DependencyProperty NameProperty =
        DependencyProperty.Register("Name", typeof(string), typeof(DocumentVm), new PropertyMetadata(null, new PropertyChangedCallback( (s, e)=>
            {
                // This will run when the property is set.
            })));        
}
于 2012-12-26T01:05:40.130 回答