1

我有一个 wpf (.Net 4.5) 数据网格。我将 MVVM 模式用于我的应用程序和 MVVM-Light 框架。

我有一个数据网格,它绑定到一个名为 TrackingCollection 的“跟踪”对象的可观察集合。数据网格 selectedItem 绑定到 viewModel 中的“SelectedTracking”属性。

<DataGrid Grid.Column="1" Grid.Row="3" Grid.ColumnSpan="3" MinHeight="300"          
          ItemsSource="{Binding TrackingCollection}"    
          CanUserAddRows="False" CanUserDeleteRows="False"
          SelectionMode="Single" SelectedItem="{Binding SelectedTracking, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
           RowDetailsTemplate="{StaticResource FTC_TrackingFullDetailTemplate}">
</DataGrid>

我在一列中有一个组合框,它绑定到 SelectedTracking 对象的“idAction”属性。当用户更改此组合框的选择时,我想在数据网格的另外两列中分配另外两个组合框的值。这些其他列不绑定到视图模型的属性,而是直接绑定到 SelectedTracking 对象的属性。SelectedTracking 对象的这些属性是 iSource_Type 和 iDestination_Type。

这是 iSourceType 的列定义:

<DataGridTemplateColumn Header="SOURCE" SortMemberPath="tracking_source.chrSource" >
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox Style="{StaticResource FTC_DetailComboBox}"  Margin="0" Padding="3"
                ItemsSource="{Binding DataContext.TrackingSources, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
                SelectedValuePath="idSource"
                DisplayMemberPath="chrSource"
                SelectedValue="{Binding iSource_Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
            </ComboBox>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

因此,当我在 ViewModel 代码中(在第一个“Action”comboBox 的 selectionChanged 函数中)分配这些 (iSource_Type, iDestination_Type) 值时,这些值会在对象本身上更新。但是更改不会反映回绑定到这些属性的 UI 组合框。

我尝试了什么:

第一的:

我有一个 INotifyPropertyCHanged 的​​实现,它带有一个名为 RaisePropertyChanged 的​​函数。这是通过 MVVM_Light 框架提供的。所以我尝试使用以下内容:

RaisePropertyChanged("iDestination_Type")
RaisePropertyChanged("iSource_Type")
RaisePropertyChanged("SelectedTracking")
RaisePropertyChanged("SelectedTracking.iDestination_Type")
RaisePropertyChanged("SelectedTracking.iSource_Type")

但这些都行不通。

第二:

我还尝试在绑定到 SelectedTracking 对象的视图模型中创建属性。但这只是导致所有跟踪对象获得相同的值。

问题: INotifyPropertyChanged 能否对不属于视图模型的属性起作用,但它们是在视图模型中找到的对象的属性。如果是这样,我在 INotifyPropertyChanged 事件中需要什么语法?

附加信息: INotifyPropertyChanged (RaisePropertyChanged()) 的 MVVM-Light 实现不接受通常会更新所有 UI 元素的空字符串。那么有没有一种方法可以在一个类中覆盖 INotifyPropertyCHanged 的​​实现?

4

1 回答 1

2

如果我正确理解您的问题,您想要一种方法来通知您的 ViewModel 更改您的模型。

如果是这样,您可以在模型中实现 INotifyPropertyChanged 并订阅 ViewModel 中的模型对象 PropertyChanged 事件。在这里,您可以在 ViewModel 属性上提出属性更改通知。

一个简单的例子来演示这个概念:

模型:

public class Tracking : INotifyPropertyChanged
{
    private string _isourcetype;
    private string _idestinationtype;

    public string SourceType
    {
        get { return _isourcetype; }
        set
        {
            _isourcetype = value;
            OnPropertyChanged("SourceType");
        }
    }

    public string DestinationType
    {
        get { return _idestinationtype; }
        set
        {
            _idestinationtype = value;
            OnPropertyChanged("DestinationType");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

视图模型:

public class TrackingViewModel : ViewModelBase
{
    private Tracking _selectedTracking;

    public string DestinationType
    {
        get { return _selectedTracking.DestinationType; }
    }

    public string SourceType
    {
        get { return _selectedTracking.SourceType; }
    }

    public Tracking SelectedTracking
    {
        get { return _selectedTracking; }
        set
        {
            _selectedTracking = value;
            RaisePropertyChanged("SelectedTracking");
        }
    }

    public TrackingViewModel()
    {
        _selectedTracking = new Tracking();
        _selectedTracking.PropertyChanged += SelectedTracking_PropertyChanged;
    }

    void SelectedTracking_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "SourceType":
                RaisePropertyChanged("SourceType");
                break;
            case "DestinationType":
                RaisePropertyChanged("DestinationType");
                break;
        }
    }
}
于 2013-06-14T20:01:32.100 回答