【WPF】XmlDataProvider级联
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【WPF】XmlDataProvider级联,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4618字,纯文字阅读大概需要7分钟。
内容图文
![【WPF】XmlDataProvider级联](/upload/InfoBanner/zyjiaocheng/1319/99a6fb74ba0f4bccbb23bc64903b0ad4.jpg)
首先在绑定的时候进行转换:
public class RegionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var name = value asstring; var filter = parameter asstring; if (string.IsNullOrEmpty(name) && filter != "country") { returnnull; } var provider = new XmlDataProvider(); provider.Source = new Uri("Resources/Region.xml", UriKind.Relative); if (filter == "country") { provider.XPath = "/region/country/@name"; } elseif (filter == "province") { provider.XPath = string.Format("/region/country[@name=‘{0}‘]/province/@name", name); } elseif (filter == "city") { provider.XPath = string.Format("/region/country/province[@name=‘{0}‘]/city/@name", name); } elseif (filter == "town") { provider.XPath = string.Format("/region/country/province/city[@name=‘{0}‘]/town/@name", name); } return provider; } publicobject ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { thrownew NotImplementedException(); } }
再看以下如何绑定的
<converters:RegionConverter x:Key="region"/>
<ComboBox Grid.Column="0" x:Name="country" DataContext="{Binding Converter={StaticResource region}, ConverterParameter=country}" SelectedValue="{Binding DataContext.CurrEditorItem.Country,UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}" ItemsSource="{Binding}" Width="85" Style="{StaticResource CommonComboBoxStyle}" /> <ComboBox Grid.Column="2" x:Name="province" DataContext="{Binding SelectedValue, ElementName=country, Converter={StaticResource region}, ConverterParameter=province}" SelectedValue="{Binding DataContext.CurrEditorItem.Province,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}" ItemsSource="{Binding}" Width="85" Style="{StaticResource CommonComboBoxStyle}" /> <ComboBox Grid.Column="4" x:Name="city" DataContext="{Binding SelectedValue, ElementName=province, Converter={StaticResource region}, ConverterParameter=city}" SelectedValue="{Binding DataContext.CurrEditorItem.City,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}" ItemsSource="{Binding}" Width="85" Style="{StaticResource CommonComboBoxStyle}" /> <ComboBox Grid.Column="6" x:Name="town" DataContext="{Binding SelectedValue, ElementName=city, Converter={StaticResource region}, ConverterParameter=town}" SelectedValue="{Binding DataContext.CurrEditorItem.Area,UpdateSourceTrigger=PropertyChanged,RelativeSource={RelativeSource AncestorType={x:Type mui:ModernWindow}}}" ItemsSource="{Binding}" Text="{Binding Area,UpdateSourceTrigger=PropertyChanged}" Width="85" Style="{StaticResource CommonComboBoxStyle}" /> <TextBlock Grid.Column="0" Text="国家" Tag="{Binding SelectedValue, ElementName=country}" Style="{StaticResource TipTextBlock}"/> <TextBlock Grid.Column="2" Text="省份" Tag="{Binding SelectedValue, ElementName=province}" Style="{StaticResource TipTextBlock}"/> <TextBlock Grid.Column="4" Text="市/区" Tag="{Binding SelectedValue, ElementName=city}" Style="{StaticResource TipTextBlock}"/> <TextBlock Grid.Column="6" Text="县/镇" Tag="{Binding SelectedValue, ElementName=town}" Style="{StaticResource TipTextBlock}"/>
TextBlock放在ComboBox上面,Textblock样式如下
<Style x:Key="TipTextBlock" TargetType="{x:Type TextBlock}"> <Setter Property="IsHitTestVisible" Value="False" /> <Setter Property="HorizontalAlignment" Value="Left"/> <Setter Property="VerticalAlignment" Value="Center"/> <Setter Property="Margin" Value="12,0,0,0"/> <Setter Property="Opacity" Value="0"/> <Style.Triggers> <Trigger Property="Tag" Value="{x:Null}"> <Setter Property="Opacity" Value="1"/> </Trigger> </Style.Triggers> </Style>
枚举在WPF的应用:
<ComboBox x:Name="cbbDataType" ItemsSource="{Binding Source={StaticResource InfoDetailTypeItems}}" SelectedItem="{Binding CurrEditorItem.DataType, ValidatesOnDataErrors=True}" ItemTemplate="{StaticResource InfoDetailTypeDataTemplate}" Grid.Row="2" Grid.Column="1" Style="{StaticResource EditorComboBoxStyle}" />
<x:Array x:Key="InfoDetailTypeItems" Type="{x:Type adservice:ShowDataType}"> <adservice:ShowDataType>Image</adservice:ShowDataType> <adservice:ShowDataType>Video</adservice:ShowDataType> <adservice:ShowDataType>ThreeDModel</adservice:ShowDataType> </x:Array>
原文:http://www.cnblogs.com/wywnet/p/3818951.html
内容总结
以上是互联网集市为您收集整理的【WPF】XmlDataProvider级联全部内容,希望文章能够帮你解决【WPF】XmlDataProvider级联所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】