【WPF】XmlDataProvider級聯

來源:互聯網
上載者:User

標籤:style   blog   color   width   for   io   

首先在綁定的時候進行轉換:

  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();        }    }

再看以下如何綁定的

 <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>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.