2017-11-21 7 views
1

관찰 가능한 컬렉션에 바인딩 된 데이터 표가 있습니다. 단추에 mouse-enter 이벤트를 가져 와서 데이터베이스에서 검색 한 일부 내용을 표시하고 싶습니다.MVVM 바인딩 popover가 DataGrid에 열려 있음

효율성을 높이기 위해이 데이터를 마우스 오버시에 가져오고 싶으므로 초기 렌더링이 빨라집니다.

나는 ViewModel의 ICommand에 마우스 입력 및 마우스 놓기 이벤트를 바인딩했습니다. 이 콘솔은 마우스 입력, 마우스 왼쪽 행 ID를 정확하게 기록합니다 (간략하게하기 위해 마우스 왼쪽을 생략)

수동으로 isOpen = "true"를 입력하면 모든 popovers가 예상대로 표시됩니다.

내가 가진 문제는 위임 명령 모음에서 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; } 
    … 
} 

뷰 모델

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)); 
} 

데이터 그리드 열

<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를 구현하고 있습니다. 조건은 xaml에 바인딩 된 ObservableCollection에서 사용되는 간단한 클래스입니다. 따라서 관찰 가능한 컬렉션이 변경되고 PropertyChangedEventHandler가 있습니다. – pgee70