我真的是 WPF 的新手,我需要你的帮助。我的应用程序允许用户检查大陆并查看包含的国家/地区。Country 有两个属性:名称和区域。问题是我需要显示所有大陆国家的平均面积。我的数据模型如下所示:
<XmlDataProvider x:Key="CountryStoreDataSource" XPath="CountryStore">
<x:XData>
<CountryStore xmlns="">
<Continents Continent="Europe">
<Countries Country="Italy" Area="300"/>
<Countries Country="Iceland" Area="350"/>
</Continents>
<Continents Continent="Asia">
<Countries Country="China" Area="700"/>
<Countries Country="India" Area="650"/>
</Continents>
<Continents Continent="Africa">
<Countries Country="South Africa" Area="550"/>
<Countries Country="Egypt" Area="500"/>
</Continents>
</CountryStore>
</x:XData>
</XmlDataProvider>
我也有模板将列表框与我的数据模型连接起来:
<Grid.Resources>
<DataTemplate x:Key="countryItemTemplate">
<Label Content="{Binding XPath=@Country}"/>
</DataTemplate>
<DataTemplate x:Key="areaItemTemplate">
<Label Content="{Binding XPath=@Area}"/>
</DataTemplate>
</Grid.Resources>
最后我有我的列表框的实现:
<ListBox
Grid.Row="1"
ItemsSource="{Binding XPath=Countries}"
ItemTemplate="{StaticResource countryItemTemplate}"
Margin="0,0,0,0" />
<ListBox
Grid.Row="1"
ItemsSource="{Binding XPath=Countries}"
ItemTemplate="{StaticResource areaItemTemplate}"
Margin="0,0,0,0"
Grid.Column="1"
Name="listBoxAreas"
/>
实际上我不知道如何从 c# 代码中的列表框中获取我的值,有没有办法获取值并在 xml 中对它们进行处理?谢谢你。