2017-11-21 6 views
1

私は観測可能なコレクションにバインドされたデータグリッドを持っています。私はボタンにmouse-enterイベントを持たせて、データベースから取り出されたいくつかのコンテンツを表示したいと思います。MVVMバインディングポップオーバーは、データグリッド内のオープンです。

効率を向上させるために、マウスのホバーでこのデータを取得したいので、最初のレンダリングが高速になります。

私は、ViewModelのICommandにマウス入力イベントとマウス離脱イベントをバインドしました。これらのコンソールは、マウス入力、マウス離脱行IDを正しくログに記録します(簡略化のためにマウスを省略します)。

手動でisOpen = "true"と入力すると、すべてのポップオーバーが期待通りに表示されます。

私が持っている問題は、コマンドデリゲートのObservableコレクションを変更した場合、データグリッドは内容を更新していないということです。 Observableコレクションはデバッガで正しいと思われます。

//Conditions.cs 
public class Condition 
{ 
    public int Id { get; set; } 
    public string Description { get; set; } 
    public bool PopupOpen { get; set; } 
    public string PopupContent { get; set; } 
    … 
} 

のViewModel

private ObservableCollection<Condition> _conditionsObservableCollection; 
    public ObservableCollection<Condition> ConditionsObservableCollection 
    { 
     get => _conditionsObservableCollection; 
     set 
     { 
      _conditionsObservableCollection = value; 
      DynamicOnPropertyChanged(); 
     } 
    } 

    private ICommand _showConditionChildrenMouseEnterCommand; 
    public ICommand ShowConditionChildrenMouseEnterCommand=> _showConditionChildrenMouseEnterCommand ?? 
     (_showConditionChildrenMouseEnterCommand = new RelayCommand<int>(ShowConditionChildren)); 

    private void ShowConditionChildren(int id) 
    { 
     Console.WriteLine("enter:"+id); // this is output correctly. 
     foreach (Condition condition in ConditionsObservableCollection) 
     { 
      condition.PopupOpen = condition.Id == id; 
     } 
     //ConditionsObservableCollection appears to be changed here. 
     OnPropertyChanged("ConditionsObservableCollection"); 
    } 

BaseViewModel

public event PropertyChangedEventHandler PropertyChanged; 
public void DynamicOnPropertyChanged([CallerMemberName] string propertyName = null) 
{ 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));   
} 

protected virtual void OnPropertyChanged(string propertyName) 
{ 
    var handler = PropertyChanged; 
    handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
} 

DataGrid列

<DataGridTemplateColumn Header="Info"> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Button Style="{StaticResource MaterialDesignFlatButton}"> 
        <i:Interaction.Triggers> 
         <i:EventTrigger EventName="MouseEnter"> 
          <i:InvokeCommandAction 
           CommandParameter="{Binding Path=Id}" 
           Command="{Binding Path=DataContext.ShowConditionChildrenMouseEnterCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
          /> 
         </i:EventTrigger> 
         <i:EventTrigger EventName="MouseLeave"> 
          <i:InvokeCommandAction 
           CommandParameter="{Binding Path=Id}" 
           Command="{Binding Path=DataContext.HideConditionChildrenMouseLeaveCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}" 
          /> 
         </i:EventTrigger> 
        </i:Interaction.Triggers> 
        <materialDesign:PackIcon Kind="InformationVariant"/> 
       </Button> 
       <Popup 
        HorizontalAlignment="Left" 
        VerticalAlignment="Bottom" 
        IsOpen="{Binding PopupOpen}"> 
        <StackPanel Background="AntiqueWhite"> 
         <TextBlock Padding="5">Here is a popup for id: <Run Text="{Binding Id}"/></TextBlock> 
        </StackPanel> 
       </Popup> 
      </StackPanel> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn> 

答えて

0
public class Condition : INotifyPropertyChanged 
{ 
    public void SetPropertyChanged(string propertyName) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 
    public int Id { get; set; } 
    public string Description { get; set; } 
    private bool popUpOpen; 
    public bool PopUpOpen 
    { 
     get { return popUpOpen; } 
     set 
     { 
      popUpOpen = value; 
      OnPropertyChanged("PopUpOpen"); 
     } 
    } 
    public string PopupContent { get; set; } 
    … 
} 

あなたのクラスにもINotifyPropertyChangedを実装する必要があります。あなたのビューモデルは、ObservableCollectionの項目への変更のみを、追加および削除された項目に関して通知します。内部プロパティの変更を通知する場合は、これを行う必要があります

0

条件がINotifyPropertyChangedのを実装する必要があります。..

+0

残念ですが、私は理解できません。ビューモデルはINotifyPropertyChangedを実装しています。 Conditionは、xamlにバインドされたObservableCollectionによって使用される単純なクラスです。したがって、観測可能なコレクションは変更され、PropertyChangedEventHandlerを持ちます。 – pgee70

関連する問題