WPF 静态资源转换器

1、使用  

    <Window.Resources>
        <Func:StringToUriConverter  x:Key="StringToUriConverter"></Func:StringToUriConverter>
    </Window.Resources>

<Hyperlink  NavigateUri="{Binding Source={StaticResource NavigateUri}, Converter={StaticResource StringToUriConverter}}" Foreground="{StaticResource WarnFontColor}" Command="{Binding HLinkCommand}" 
                                 CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" >
                        <TextBlock Text="{StaticResource UriInfo}"/>
                    </Hyperlink>

 2、资源

<s:String x:Key="NavigateUri">https://www.gynova.cn/</s:String>

3、转换器 

  public class StringToUriConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Uri uri = null;
            if (value != null)
            {
                uri = new Uri(value.ToString());
            }
            return uri;

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException("The method or operation is not implemented.");
        }
    }

猜你喜欢

转载自blog.csdn.net/xionglifei2014/article/details/124636820
今日推荐