我有一個WPF DataGrid綁定到我的XML文件中的一些節點。WPF DataGrid綁定到XML不更新
<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:
<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>
下面是一些後臺代碼:
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中DataGrid。這是因爲ItemsSource XPath從配置文件節點讀取「ActiveProfile」值,並使用它來選擇適當的配置文件節點。即使在設計模式下,DataGrid也會顯示這些值。
在運行時,我將ComboBox從第二個項目(索引1)更改爲第一個項目(索引0)。代碼停止在SelectionChanged事件中,因此我可以輪詢「ActiveProfile」值,該值從「1」正確變爲「0」,因爲ComboBox的綁定模式是TwoWay。不幸的是,DataGrid不會像它應該那樣重新顯示項目1,2和3。
我的MainWindow_Closing事件具有保存XML文件(如果已更改)的代碼。如果我這樣做,那麼下一次應用程序運行時,DataGrid值將從1,2和3開始。因此綁定正在工作,並從XML文件中選擇適當的項目列表,但DataGrid只是不更新下次加載。
我認爲XmlDataProvider會自動通知DataGrid刷新。我試着在SelectedIndexChanged事件中做一個DataGrid.Items.Refresh(),但沒有運氣。
有沒有人有任何想法爲什麼我的DataGrid不會從XML刷新?謝謝...