First, perform conversion during binding:
public class RegionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { var name = value as string; var filter = parameter as string; if (string.IsNullOrEmpty(name) && filter != "country") { return null; } var provider = new XmlDataProvider(); provider.Source = new Uri("Resources/Region.xml", UriKind.Relative); if (filter == "country") { provider.XPath = "/region/country/@name"; } else if (filter == "province") { provider.XPath = string.Format("/region/country[@name=‘{0}‘]/province/@name", name); } else if (filter == "city") { provider.XPath = string.Format("/region/country/province[@name=‘{0}‘]/city/@name", name); } else if (filter == "town") { provider.XPath = string.Format("/region/country/province/city[@name=‘{0}‘]/town/@name", name); } return provider; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
See how to bind the following
<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 = "country" tag = "{binding selectedvalue, elementname = country}" style = "{staticresource tiptextblock}"/> <textblock grid. column = "2" text = "Province" tag = "{binding selectedvalue, elementname = province}" style = "{staticresource tiptextblock}"/> <textblock grid. column = "4" text = "city/District" tag = "{binding selectedvalue, elementname = city}" style = "{staticresource tiptextblock}"/> <textblock grid. column = "6" text = "/" tag = "{binding selectedvalue, elementname = town}" style = "{staticresource tiptextblock}"/>
Textblock is placed on the ComboBox, And the textblock style is as follows:
<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>
Enumeration applications in 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>