2013-06-05 7 views
1

XML 파일의 일부 노드에 바인딩 된 WPF DataGrid가 있습니다. 나는 응용 프로그램을 실행할 때XML에 바인딩 된 WPF DataGrid가 업데이트되지 않습니다.

Private settingsDoc As XmlDocument 
Private dp As XmlDataProvider 

Private Sub MainWindow_Initialized(sender As Object, e As System.EventArgs) Handles Me.Initialized 

    dp = Me.TryFindResource("MySettings") 
    If dp IsNot Nothing Then 
     settingsDoc = dp.Document 
     settingsText = settingsDoc.OuterXml 
    End If 

End Sub 

Private Sub cboProfile_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles cboProfile.SelectionChanged 

    If Me.dgPIDData IsNot Nothing Then 
     If dp IsNot Nothing Then 
      Stop 
     End If 
    End If 

End Sub 

이 제대로 항목 4, 5, 6을 보여줍니다 여기에

<XmlDataProvider x:Name="MySettings" x:Key="MySettings" Source="Settings.xml" XPath="Settings" IsAsynchronous="False" IsInitialLoadEnabled="True"/> 
<XmlDataProvider x:Name="PIDData" x:Key="PIDData" Source ="Settings.xml" XPath="Settings/Profiles"/> 

<ComboBox Name="cboProfile" Width="150" Padding="10,3,4,3" ItemsSource="{Binding Source={StaticResource MySettings}, XPath=Profiles/Profile}" 
      SelectedValuePath="@Id" DisplayMemberPath="@Desc" 
      SelectedValue="{Binding Mode=TwoWay, Source={StaticResource MySettings}, XPath=Profiles/@ActiveProfile}"/> 

<DataGrid Name="dgPIDData" AutoGenerateColumns="False" Height="201" HorizontalAlignment="Left" Margin="233,137,0,0" VerticalAlignment="Top" Width="444" 
      DataContext="{Binding Source={StaticResource PIDData}}" ItemsSource="{Binding XPath=Profile[@Id\=../@ActiveProfile]/PIDs/PID}"> 
    <DataGrid.Columns> 
     <DataGridTextColumn Header="Address" Binding="{Binding [email protected]}"/> 
     <DataGridTextColumn Header="Set Point" Binding="{Binding [email protected]}"/> 
     <DataGridTextColumn Header="Proc Val" Binding="{Binding [email protected]}"/> 
    </DataGrid.Columns> 
</DataGrid> 

일부 코드 숨김입니다 : 여기
<Settings xmlns=""> 
    <Profiles xmlns="" ActiveProfile="1"> 
    <Profile Id="0" Desc="- none -" Port="0" Baud="0" DataBits="0" Parity="0" StopBits="0" CharDelay="0" ReadTimeout="0" ProcInterval="0" SetInterval="0" DecInterval="0" ChartInterval="0" SaveInterval="0"> 
     <PIDs> 
     <PID Address="1" SetPoint="one" ProcVal="one" /> 
     <PID Address="2" SetPoint="two" ProcVal="two" /> 
     <PID Address="3" SetPoint="three" ProcVal="three" /> 
     </PIDs> 
    </Profile> 
    <Profile Id="1" Desc="Test Profile 1" Port="1" Baud="19200" DataBits="7" Parity="0" StopBits="3" CharDelay="1" ReadTimeout="100" ProcInterval="100" SetInterval="0" DecInterval="0" ChartInterval="0" SaveInterval="0"> 
     <PIDs> 
     <PID Address="4" SetPoint="four" ProcVal="four" /> 
     <PID Address="5" SetPoint="five" ProcVal="five" /> 
     <PID Address="6" SetPoint="six" ProcVal="six" /> 
     </PIDs> 
    </Profile> 

어떤 관련 XAML입니다 DataGrid. 이것은 ItemsSource XPath가 Profiles 노드에서 "ActiveProfile"값을 읽고이를 사용하여 적절한 Profile 노드를 선택하기 때문입니다. 디자인 모드에서도 DataGrid는 이러한 값을 표시합니다.

런타임에 ComboBox를 두 번째 항목 (인덱스 1)에서 첫 번째 항목 (인덱스 0)으로 변경합니다. ComboBox의 바인딩 모드가 TwoWay이므로 "1"에서 "0"으로 바뀌는 "ActiveProfile"값을 폴링 할 수 있도록 코드가 SelectionChanged 이벤트에서 중지됩니다. 불행히도 DataGrid는 항목 1, 2 및 3을 다시 표시하지 않아야합니다.

내 MainWindow_Closing 이벤트에는 변경된 XML 파일을 저장하는 코드가 있습니다. 이렇게하면 다음 번에 응용 프로그램이 실행될 때 DataGrid 값이 1, 2 및 3에서 시작됩니다. 따라서 바인딩이 작동하고 XML 파일에서 적절한 항목 목록을 선택하지만 DataGrid는 업데이트하지 않습니다. 다음에로드 할 때.

XmlDataProvider가 자동으로 DataGrid에 새로 고침을 알리는 것으로 생각했습니다. SelectedIndexChanged 이벤트에서 DataGrid.Items.Refresh()를 시도했지만 행운이 없습니다.

아무도 내 DataGrid가 XML에서 새로 고치지 않는 이유를 알고 있습니까? 감사합니다 ...

답변

0

직접 고쳐 봤습니다. 왜 그런지 모르겠지만이 코드를 쓰면 :

Private Sub settingsDoc_NodeChanged(sender As Object, e As System.Xml.XmlNodeChangedEventArgs) Handles settingsDoc.NodeChanged 

    Dim dp As XmlDataProvider = DirectCast(Me.TryFindResource("PIDData"), XmlDataProvider) 
    If dp IsNot Nothing Then 
     dp.Document = settingsDoc 
    End If 

End Sub 

이 문제를 해결하는 것 같습니다. 이 코드를 실행하기 전에 settingsDoc 및 XmlDataProvider.Document가 모두 적절한 값을 반영하기 때문에 필요하지 않아야합니다. 어떤 이유로 위의 코드는 DataGrid를 강제로 새로 고칩니다.

관련 문제