2012-01-11 4 views
0

WPF MVVM Pattern을 사용하고 있습니다. 내보기에서 2 ListBoxes 및 DataGrid가 있습니다. SQL Server에서 데이터를 가져 오기 위해 EntityFramework를 사용하고 있습니다. 내 뷰 모델은 내 LisBoxes이 채워 수 있어요이양방향 바인딩이 제대로 작동하지 않습니다.

<Window x:Class="SingleAppLogMVVM.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:vm="clr-namespace:SingleAppLogMVVM" 
    Title="MainWindow" Height="800" Width="1000"> 
<Window.DataContext > 
    <vm:DetailsViewModel /> 
</Window.DataContext> 
<Grid> 
    <ListBox Height="200" HorizontalAlignment="Left" Margin="50,50,0,0" Name="fTypeListBox" VerticalAlignment="Top" Width="125" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding Categories}" SelectedItem="{Binding SelectedType, Mode=TwoWay}" > 
     <ListBox.ItemTemplate > 
      <DataTemplate > 
       <DockPanel Width="120" LastChildFill="True" > 
        <TextBlock Text="{Binding Type, Mode=TwoWay}" Width="110" Margin="5" /> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <ListBox Height="Auto" HorizontalAlignment="Left" Margin="50,275,0,50" Name="fUserListBox" VerticalAlignment="Stretch" Width="125" SelectionMode="Single" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding SystemNames}" SelectedItem="{Binding SelectedUser, Mode=TwoWay}" > 
     <ListBox.ItemTemplate > 
      <DataTemplate > 
       <DockPanel Width="120" LastChildFill="True" > 
        <TextBlock Text="{Binding User, Mode=TwoWay}" Width="110" Margin="5" /> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <DataGrid AutoGenerateColumns="False" Height="350" HorizontalAlignment="Stretch" Margin="225,50,50,0" IsReadOnly="True" Width="Auto" 
       VerticalAlignment="Top" CanUserReorderColumns="False" CanUserResizeColumns="False" Name="fDGrid" VirtualizingStackPanel.IsVirtualizing="True" 
       CanUserResizeRows="False" IsManipulationEnabled="True" RowHeight="35" SelectionMode="Single" VirtualizingStackPanel.VirtualizationMode="Recycling" 
       ItemsSource="{Binding DContext, Mode=OneWay}"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}" Width="5*" /> 
      <DataGridTextColumn Header="IID" Binding="{Binding Path=IID}" Width="5*" /> 
      <DataGridTextColumn Header="INSTANCEID" Binding="{Binding Path=INSTANCEID}" Width="10*" /> 
      <DataGridTextColumn Header="TYPE" Binding="{Binding Path=TYPE}" Width="10*" /> 
      <DataGridTextColumn Header="SOURCE" Binding="{Binding Path=SOURCE}" Width="10*" /> 
      <DataGridTextColumn Header="TIME" Binding="{Binding Path=TIME}" Width="10*" /> 
      <DataGridTextColumn Header="SNAME" Binding="{Binding Path=SNAME}" Width="10*" /> 
      <DataGridTextColumn Header="MESSAGE" Binding="{Binding Path=MESSAGE}" Width="15*" /> 
      <DataGridTextColumn Header="ACTIONS" Binding="{Binding Path=ACTIONS}" Width="15*" /> 
      <DataGridTextColumn Header="CLEARED ON" Binding="{Binding Path=CLEAREDON}" Width="10*" /> 
     </DataGrid.Columns> 
    </DataGrid>   
</Grid> 

처럼이

private Types _type; 
    private Users _user; 
    private ObjectResult<APP> _dataContext; 

    public IEnumerable<Types> Categories 
    { 
     get; 
     private set; 
    } 

    public IEnumerable<Users> SystemNames 
    { 
     get; 
     private set; 
    } 

    public Types SelectedType 
    { 
     get 
     { 
      return _type; 
     } 
     set 
     { 
      _type = value; 
      RaisePropertyChanged("SelectedType"); 
     } 
    }  

    public Users SelectedUser 
    { 
     get 
     { 
      return _user; 
     } 
     set 
     { 
      _user = value; 
      RaisePropertyChanged("SelectedUser"); 
     } 
    } 

    public ObjectResult<APP> DContext 
    { 
     get 
     { 
      return _dataContext; 
     } 
     set 
     { 
      _dataContext = value; 
      RaisePropertyChanged("DContext"); 
     } 
    } 

    public ObjectResult<APP> GetDataContext() 
    { 
     AppLogEntities context = new AppLogEntities(); 
     return context.GetAppLog(SelectedUser.User, SelectedType.Type); 
    } 

    public DetailsViewModel() 
    { 
     Categories = new List<Types> 
     { 
      new Types{Type = "All"}, 
      new Types{Type = "Information"}, 
      new Types{Type = "Warning"}, 
      new Types{Type = "Error"} 
     }; 

     SystemNames = new List<Users> 
     { 
      new Users{User = "All"}, 
      new Users{User = "SYS_01"}, 
      new Users{User = "SYS_02"} 
     }; 

     SelectedType = new Types(); 
     SelectedUser = new Users(); 

     DContext = GetDataContext(); 
    }` 

그리고 내보기 liiks 것 같습니다. 또한 내 데이터 그리드가 수동으로 뷰 모델 코드가리스트 박스에서 내 선택에 따라 작동하지 않는 이유를 이해가 안

public ObjectResult<APP> GetDataContext() 
    { 
     AppLogEntities context = new AppLogEntities(); 
     return context.GetAppLog(SelectedUser.User, SelectedType.Type); 
    } 

에 플러그인 값 경우 채워 할 수 있어요. 제발 도와주세요.

+0

주 후에는 결과를 얻을하는 데 필요한 몇 가지 : 당신은 dataContex 공공 액세스 권한을 부여해서는 안된다. 비동기 적으로 쿼리를 호출하는 것은 좋지 않습니다. 앱을 차단하고 많은 문제를 초래할 수 있습니다. –

+0

_dataContext를 private로 선언했습니다. 그 밖의 무엇을해야합니까? GetDataContext 메서드에 값을 수동으로 넣으면 DataGrid Populated를 가져올 수 있습니다. –

답변

1

작은 변화가

public Types SelectedType 
{ 
    get 
    { 
     return _type; 
    } 
    set 
    { 
     _type = value; 
     RaisePropertyChanged("SelectedType"); 
     DContext = GetDataContext(); //refresh the data 
    } 
}  

public Users SelectedUser 
{ 
    get 
    { 
     return _user; 
    } 
    set 
    { 
     _user = value; 
     RaisePropertyChanged("SelectedUser"); 
     DContext = GetDataContext(); //refresh the data 
    } 
} 


public DetailsViewModel() 
{ 
    Categories = new List<Types> 
    { 
     new Types{Type = "All"}, 
     new Types{Type = "Information"}, 
     new Types{Type = "Warning"}, 
     new Types{Type = "Error"} 
    }; 

    SystemNames = new List<Users> 
    { 
     new Users{User = "All"}, 
     new Users{User = "SYS_01"}, 
     new Users{User = "SYS_02"} 
    }; 

    _type = new Types(); //use the field rather than the property so GetDatacontenxt doesnt get called multiple times 
    _user = new Users(); 
    DContext = GetDataContext(); 
} 
+0

고맙습니다. 지금 일하고있다. –

1

보기 모델에서 SelectedUserSelectedType 속성을 변경할 때마다 수동으로 GetDataContext()을 호출해야합니다. 이 속성을 설정하는 것이 좋습니다. 비록 GetDataContext()이 장기 실행 작업이면 비동기 적으로 수행해야 결과가로드되는 동안 UI가 멈추지 않습니다.

+0

조금 설명해 주시겠습니까? 저는 WPF MVVM에서 초보자입니다. –

관련 문제